[leetcode 285] Inorder Successor in BST---查找二叉搜索树中某个节点在中序遍历中的后续节点
来源:互联网 发布:mysql数据库篡改 黑客 编辑:程序博客网 时间:2024/06/05 20:53
Question:
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, return null
.
题目意思是给定一个二叉搜索树和在这个搜索树当中的一个节点P,求在中序遍历中p的后续节点。
可以知道:
1、如果这个节点p有右孩子,那么p的后续节点为右子树的的最左值(即最后一个没有左孩子的节点);
2、如果节点p没有右孩子,那就在二叉搜索树中搜索节点p,并在从上往下的查找过程中依次更新记录比p的val值大的节点。
代码如下(参考):
<span style="font-size:14px;">class Solution {public: TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) { if (p -> right) return leftMost(p -> right); TreeNode* suc = NULL; while (root) { if (p -> val < root -> val) { suc = root; root = root -> left; } else if (p -> val > root -> val) root = root -> right; else break; } return suc; }private: TreeNode* leftMost(TreeNode* node) { while (node -> left) node = node -> left; return node; }};</span>
0 0
- [leetcode 285] Inorder Successor in BST---查找二叉搜索树中某个节点在中序遍历中的后续节点
- Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点
- Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup
- leetcode 230. Kth Smallest Element in a BST 二叉搜索树BST的中序遍历
- 在一个二叉排序树(BST)(left<right)中查找某个节点的后序节点
- 如何用C++递归来查找BST中的根节点的inorder successor
- **LeetCode-Inorder Successor in BST
- [leetcode]Inorder Successor in BST
- leetcode Inorder Successor in BST
- [LeetCode]Inorder Successor in BST
- 【BST】在搜索二叉树中,寻找节点数据域在[L,U]范围内的节点
- 【二叉树】寻找一个二叉树的节点在中序遍历中的后继节点
- leetcode 501. Find Mode in Binary Search Tree 二叉搜索树BST的中序遍历
- 找二叉树中指定节点在中序遍历中的下一个节点
- 《程序员面试金典》--寻找二叉树中某个节点中序遍历的下一个节点
- LeetCode 题解(266) : Inorder Successor in BST
- Leetcode - 285.Inorder Successor in BST
- LeetCode 285. Inorder Successor in BST
- OpenCV简介及图像处理示例教程
- DIY文字闪动效果的TextView
- 关于label等uiview的圆角问题
- 卸载软件时出现"Windows Installer 软件包有问题"问题
- 关于HandlerThread类的学习心得
- [leetcode 285] Inorder Successor in BST---查找二叉搜索树中某个节点在中序遍历中的后续节点
- DVI接口
- C++ 排序函数 sort(),qsort()的用法
- Logger java日志处理
- 关于使用node进行网站前后端分离设想和问题解决
- 关于边界的问题
- 图的邻接矩阵
- 大志非才不就,大才非学不成—博文资源汇总
- 三和先生遺稿 1967年