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 解答的更多相关文章

  1. [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 ...

  2. LeetCode Inorder Successor in BST

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst/ Given a binary search tree and a nod ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. 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. 本题 ...

  7. 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 ...

  8. [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 ...

  9. LeetCode 510. Inorder Successor in BST II

    原题链接在这里:https://leetcode.com/problems/inorder-successor-in-bst-ii/ 题目: Given a binary search tree an ...

随机推荐

  1. JFrame画图基础和事件监听

    消息框 JOptionPane.showMessageDialog(mine.this, "删除不成功!"); 画图 class MyJPanel extends JPanel / ...

  2. PHP null常量和null字节的区别

    在学习isset()时,看到了这句话:“如果已经使用 unset() 释放了一个变量之后,它将不再是 isset().若使用 isset() 测试一个被设置成 NULL 的变量,将返回 FALSE.同 ...

  3. javaweb 登录注册

    1:用户登录界面 login.jsp <%@ page language="java" import="java.util.*" pageEncoding ...

  4. hdu 5422 Rikka with Graph(简单题)

    Problem Description As we know, Rikka is poor at math. Yuta is worrying about this situation, so he ...

  5. Laravel-表单篇-零散信息

    1.asset('path'):用于引入静态文件,包括css.js.img 2.分页,调用模型的paginate(每页显示的行数)方法, 如$student = Student::paginate(2 ...

  6. UESTC 1811 Hero Saving Princess

    九野的博客,转载请注明出处 http://blog.csdn.net/acmmmm/article/details/11104265 题目链接 :http://222.197.181.5/proble ...

  7. 【转载】cocos2d-x2.2.3和android的平台环境

    这两天试图按照教程来学习写游戏移植到的横版过关Android在.在网上找了很多教程,但版本号变化.所使用的工具有细微的差别.所以,现在我们还没有准备好,阅读后,下面的文章.最后能够顺利您的手机上跑起来 ...

  8. poj 3009 Curling 2.0 (dfs )

    Curling 2.0 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11879   Accepted: 5028 Desc ...

  9. OC-字典&数组运用实例:通讯录的实现

    需求实现: 一.定义联系⼈类ContactPerson 实例变量:姓名.性别.电话号码.住址.分组名称. 方法:初始化⽅方法(姓名.电话号码).显⽰示联系⼈信息 二.定义AddressBook类, 封 ...

  10. CSS元素 之 float

    1. float 设计的初衷 Float 设计的初衷是为了文字环绕的效果 使得文字可以围绕着 图片.就像下面这样 2. float 的包裹和 破坏 A) 包裹性 和 破坏性 例如下图   我们原本是希 ...