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 ...
随机推荐
- Best Cow Line (POJ 3617)
题目: 给定长度为N的字符串S,要构造一个长度为N的字符串T.起初,T是一个空串,随后反复进行下列任意操作. ·从S的头部删除一个字符,加到T的尾部 ·从S的尾部删除一个字符,加到T的尾部 目标是要构 ...
- Raid1源代码分析--开篇总述
前段时间由于一些事情耽搁了,最近将raid1方面的各流程整理了一遍.网上和书上,能找到关于MD下的raid1的文档资料比较少.决定开始写一个系列的关于raid1的博客,之前写过的一篇读流程也会在之后加 ...
- Codeforce 219 div1
B 4D"部分和"问题,相当于2D部分和的拓展,我是分解成2D部分和做的: f[x1][y1][x2][y2]=true/false 表示 左上(x1,y1) 右下(x2,y2)的 ...
- editplus配置详:
1:设置删除整行快捷键 2:设置背景颜色 3:php 开发环境 在 http://download.csdn.net/detail/vspeter/6002287 下载 editplus 的php语法 ...
- pyqt托盘例子
# -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...
- struts配置,略记
<!-- <listener> <listener-class>org.springframework.web.context.ContextLoaderListener ...
- JMeter录制脚本
Jmeter 是一个非常流行的性能测试工具,虽然与LoadRunner相比有很多不足,比如:它结果分析能力没有LoadRunner详细:很它的优点也有很多: l 开源,他是一款开源的免费软件,使用它你 ...
- oracle(天猫处方药留言sql)
" ?> .dtd" > <sqlMap namespace="TmallTcMessage"> <typeAlias alias ...
- html5图片标签与属性
标记: 标 记 说 明 <lmg> 图像 <Map> 图像映射 <Area> 图像映射中定义区域 <lmg>标记属性: 属 性 说 明 Src ...
- javascript构造函数+原形继承+作用域扩充
目前为止我认为这是最佳的声明对象的模式,将今天学到的东西写下 <script type="text/javascript"> function Constructor( ...