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 ...
随机推荐
- 负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体
负重前行的婚纱线上路 - i天下网商-最具深度的电商知识媒体 负重前行的婚纱线上路
- SelectSort 选择排序
//SelectSort (( O(n²))) public class TestSelectSort { public int[] selectSortArray(int[] arr){ int m ...
- syslog_test.c 简单的syslog函数
#cat syslog_test.c #include<stdio.h> #include<stdlib.h> #include<syslog.h> int mai ...
- poj 1742 Coins(dp之多重背包+多次优化)
Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar. ...
- linux shell在while中用read从键盘输入
系统是ubuntu 14.04 64bit,之前曾想安装Stream来玩dota2,但最终没成功.由于Stream只有32bit,安装Stream时也安装了大量32bit的库.删除Stream后,这些 ...
- web.xml配置DispatcherServlet
1. org.springframework.web.servlet.DispatcherServlet 所在jar包: <dependency> <groupId>org.s ...
- .net网站开发(一):1.input表单元素
其实,在半年前我对网站开发还是完全不感冒的,不是没认识,而是只认识到表面.我以为网站模型就那几样,新闻.论坛.博客啥的,仿个站出来有什么意思?但现在我是知道了,大多应用开发还是采用B/S架构的,包括服 ...
- Eclipse中Cannot find any provider supporting DES解决之道
原文出处:http://blog.csdn.net/darwinchina/article/details/12037999 异常: Caused by: java.security.NoSuchAl ...
- NSLog用法,打印日志
要输出的格式化占位: %@ 对象 %d, %i 整数 %u 无符整形 %f 浮点/双字 %x, %X 二进制整数 %o 八进制整数 %zu size_t %p 指针 %e 浮点/双字 (科 ...
- windows 删除多层文件夹
眼下目录d:\clu_1下有99个目录,名字为0,1....99,每一个目录下又有25个目录,目录名为0,1,....24,其下其下又有以日期命名的目录(如20140521),最后是zip文件.如一个 ...