Inorder Successor in BST 解答
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
.
Solution -- Iterative
Inorder result is an ascending array for BST. Thus, this problem can be transferred to find successor for a node in BST.
successor = first element that is greater than target node.
Consider two situations:
1. Target node has right child
2. Target node doesn't have right child
Trace back, find first node whose left child is in the path from target node to root.
For this situation:
1. If node has parent pointer, we just use parent pointer.
2. If node doesn't have parent pointer, we use stack.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
// Same question with finding successor in a BST
// 1. If p has right child, find minimum child in right subtree
// 2. If p doesn't have right child, find first ancestor whose left child is in the path from p to root.
TreeNode result = null;
if (p.right != null) {
result = p.right;
while (result.left != null) {
result = result.left;
}
} else {
Deque<TreeNode> stack = new LinkedList<TreeNode>();
TreeNode current = root;
// push to stack
while (current != p && current != null) {
stack.push(current);
if (p.val < current.val) {
current = current.left;
} else {
current = current.right;
}
}
// pop stack
TreeNode prev = p;
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
if (cur.left == prev) {
result = cur;
break;
}
prev = cur;
}
}
return result;
}
}
Solution 2 -- Recursive
Find Successor & Predecessor in BST
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
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...
- [Locked] Inorder Successor in BST
Inorder Successor in BST Given a binary search tree and a node in it, find the in-order successor of ...
- [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 ...
- [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. 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. 本题 ...
- 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 ...
- [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 510. Inorder Successor in BST II
原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...
随机推荐
- 个性化定制——物流app
众所周知,在互联网不断迈进的大环境下,各行各业都不免在这大潮下纷纷卷入.人们早已不再满足于传统行业,即便是所谓的新兴行业所带来的体验,他们更多的希望能够在便捷的基础上获取更加个性化的服务,个性化服务在 ...
- Android:TextView跑马灯-详解
Android:TextView跑马灯_详解 引言: TextView之所以需要跑马灯,是由于文字太长,或者是吸引眼球. 关键代码如下: android:singleLine="true&q ...
- StoryBoard 加入一个自定义View
1. 建一个 UIView的子类(MyView.h/MyView.m) 2. 建一个 View类型的XIB 3. 把xib的file‘s owner设为MyView 4. 在.h文件里加上 @prop ...
- CSS元素 之 float
1. float 设计的初衷 Float 设计的初衷是为了文字环绕的效果 使得文字可以围绕着 图片.就像下面这样 2. float 的包裹和 破坏 A) 包裹性 和 破坏性 例如下图 我们原本是希 ...
- 安装VMware Sphere ESXi 5.5
安装VMware Sphere ESXi 5.5 1.准备 待安装ESXi 5.5的机器需要大于2GB以上内存,并且支持64位和虚拟化. 下载:VMware-VMvisor-Installer-5.5 ...
- js+图片实现图片flash效果
var pic_width=685; //图片宽度 var pic_height=225; // 图片高度 var button_pos=4; //按扭位置 1左 2右 3上 4下 var stop_ ...
- Windows服务承载WCF
Source文件 ------------------------- using System; using System.Collections.Generic; using System.Linq ...
- .NET技术
1.在C#中,string str = null 与 string str = “” 请尽量使用文字或图象说明其中的区别.回答要点:说明详细的空间分配.(10分) 解:string str=null ...
- jdbc oracle 连接字符串
1.普通SID方式 jdbc:oracle:thin:username/password@x.x.x.1:1521:SID 2.普通ServerName方式 jdbc:Oracle:thin:user ...
- Trident内核中取验证码图片的几种方法
程序中用了IE的内核,想取出网站中的验证码图片,单独显示出来,调研了以下几路方法 1.枚举所有缓存文件,进行处理,找到想要的,核心代码 )//这段代码可以枚举所有缓存资源,然后对应做处理 { LPIN ...