[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST.
The successor of a node p is the node with the smallest key greater than p.val.
Example 1:
Input: root = [2,1,3], p = 1
Output: 2
Explanation: 1's in-order successor node is 2. Note that both p and the return value is of TreeNode type.
Example 2:
Input: root = [5,3,6,2,4,null,null,1], p = 6
Output: null
Explanation: There is no in-order successor of the current node, so the answer isnull.
Note:
- If the given node has no in-order successor in the tree, return
null. - It's guaranteed that the values of the tree are unique.
这道题让我们求二叉搜索树的某个节点的中序后继节点,那么根据 BST 的性质知道其中序遍历的结果是有序的,博主最先用的方法是用迭代的中序遍历方法,然后用一个 bool 型的变量b,初始化为 false,进行中序遍历,对于遍历到的节点,首先看如果此时b已经为 true,说明之前遍历到了p,那么此时返回当前节点,如果b仍为 false,看遍历到的节点和p是否相同,如果相同,此时将b赋为 true,那么下一个遍历到的节点就能返回了,参见代码如下:
解法一:
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
stack<TreeNode*> s;
bool b = false;
TreeNode *t = root;
while (t || !s.empty()) {
while (t) {
s.push(t);
t = t->left;
}
t = s.top(); s.pop();
if (b) return t;
if (t == p) b = true;
t = t->right;
}
return NULL;
}
};
下面这种方法是用的中序遍历的递归写法,需要两个全局变量 pre 和 suc,分别用来记录祖先节点和后继节点,初始化将他们都赋为 NULL,然后在进行递归中序遍历时,对于遍历到的节点,首先看 pre 和p是否相同,如果相同,则 suc 赋为当前节点,然后将 pre 赋为 root,那么在遍历下一个节点时,pre 就起到记录上一个节点的作用,参见代码如下:
解法二:
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
if (!p) return NULL;
inorder(root, p);
return suc;
}
void inorder(TreeNode *root, TreeNode *p) {
if (!root) return;
inorder(root->left, p);
if (pre == p) suc = root;
pre = root;
inorder(root->right, p);
}
private:
TreeNode *pre = NULL, *suc = NULL;
};
再来看一种更简单的方法,这种方法充分地利用到了 BST 的性质,首先看根节点值和p节点值的大小,如果根节点值大,说明p节点肯定在左子树中,那么此时先将 res 赋为 root,然后 root 移到其左子节点,循环的条件是 root 存在,再比较此时 root 值和p节点值的大小,如果还是 root 值大,重复上面的操作,如果p节点值,那么将 root 移到其右子节点,这样当 root 为空时,res 指向的就是p的后继节点,参见代码如下:
解法三:
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
TreeNode *res = NULL;
while (root) {
if (root->val > p->val) {
res = root;
root = root->left;
} else root = root->right;
}
return res;
}
};
上面那种方法也可以写成递归形式,写法也比较简洁,但是需要把思路理清,当根节点值小于等于p节点值,说明p的后继节点一定在右子树中,所以对右子节点递归调用此函数,如果根节点值大于p节点值,那么有可能根节点就是p的后继节点,或者左子树中的某个节点是p的后继节点,所以先对左子节点递归调用此函数,如果返回空,说明根节点是后继节点,返回即可,如果不为空,则将那个节点返回,参见代码如下:
解法四:
class Solution {
public:
TreeNode* inorderSuccessor(TreeNode* root, TreeNode* p) {
if (!root) return NULL;
if (root->val <= p->val) {
return inorderSuccessor(root->right, p);
} else {
TreeNode *left = inorderSuccessor(root->left, p);
return left ? left : root;
}
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/285
类似题目:
参考资料:
https://leetcode.com/problems/inorder-successor-in-bst/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] 285. Inorder Successor in BST 二叉搜索树中的中序后继节点的更多相关文章
- [LeetCode] Inorder Successor in BST 二叉搜索树中的中序后继节点
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. No ...
- [LeetCode] Inorder Successor in BST II 二叉搜索树中的中序后继节点之二
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- [Swift]LeetCode285. 二叉搜索树中的中序后继节点 $ Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. Th ...
- Leetcode 285. Inorder Successor in BST
Given a binary search tree and a node in it, find the in-order successor of that node in the BST. 本题 ...
- [LeetCode] Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- [LeetCode] 230. Kth Smallest Element in a BST 二叉搜索树中的第K小的元素
Given a binary search tree, write a function kthSmallest to find the kth smallest element in it. Not ...
- 230 Kth Smallest Element in a BST 二叉搜索树中第K小的元素
给定一个二叉搜索树,编写一个函数kthSmallest来查找其中第k个最小的元素. 注意:你可以假设k总是有效的,1≤ k ≤二叉搜索树元素个数. 进阶:如果经常修改二叉搜索树(插入/删除操作)并且你 ...
- LeetCode 230 Kth Smallest Element in a BST 二叉搜索树中的第K个元素
1.非递归解法 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...
- 530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
[抄题]: Given a binary search tree with non-negative values, find the minimum absolute difference betw ...
随机推荐
- Spring源码系列 — Bean生命周期
前言 上篇文章中介绍了Spring容器的扩展点,这个是在Bean的创建过程之前执行的逻辑.承接扩展点之后,就是Spring容器的另一个核心:Bean的生命周期过程.这个生命周期过程大致经历了一下的几个 ...
- conda opencv cv2.imshow无法使用
error: -------src-dir-------/opencv-2.4.10/modules/highgui/src/window.cpp:501: error: (-2) The funct ...
- Java匹马行天下之学编程的起点——走进编程的殿堂
学编程的起点——走进编程的殿堂 前言: 知其然,知其所以然,努力固然重要,但是思维的提升会让你事半功倍,我会用我花费时间换来的“思维”带更多的朋友入门,让你们明明白白学编程,学编程,不迷茫. 转变思维 ...
- Node.js能解决什么问题?
一.使用Node.js能解决什么问题 对于PHP.JAVA.Python等服务端语言中,为每个客户端连接创建一个新的线程,而每个线程需要大约2M的内存,理论上,具有8GB内存的服务器可以同时连接的最大 ...
- VS Code 自动修改和保存 代码风格 == eslint+prettier
最近因为用到VS Code,需要统一所有人的代码风格(前端语言js/html/css等,或者后端语言 go/python等也可以这么用). 所以参考了一些网络资料,记录下设置步骤,以便后续查阅. St ...
- 如何优雅地使用腾讯云COS-.NET篇
如何优雅地使用腾讯云COS-.NET篇 代码下载地址 https://github.com/whuanle/txypx20190809 前提 创建子账号 打开 https://console.clou ...
- Linux用户和权限——管理文件权限的命令
Linux用户和权限——管理文件权限的命令 摘要:本文主要学习了Linux中修改文件权限的命令. chown命令 chown命令,主要用于修改文件(或目录)的所有者,除此之外,这个命令也可以修改文件( ...
- sql2008好看的字体
- 基于JQuery可拖动列表格插件DataTables的踩坑记
前言 最近项目中在使用能够拖动列调整列位置顺序的表格插件---DataTables,这也是目前我找到的唯一一种存在有这种功能的插件. 在查找使用方法的过程中发现可用案例并不多,且大多言语不详.本文将全 ...
- cent OS 7 安装谷歌浏览器
我直接写一个shell 脚本, install_google.sh, bash 命令直接运行就好, 脚本内容如下: (切换root用户执行) set -e # 出错即退出 echo " ...