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.

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 public class Solution {
11     public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
12         // Same question with finding successor in a BST
13         // 1. If p has right child, find minimum child in right subtree
14         // 2. If p doesn‘t have right child, find first ancestor whose left child is in the path from p to root.
15         TreeNode result = null;
16         if (p.right != null) {
17             result = p.right;
18             while (result.left != null) {
19                 result = result.left;
20             }
21         } else {
22             Deque<TreeNode> stack = new LinkedList<TreeNode>();
23             TreeNode current = root;
24             // push to stack
25             while (current != p && current != null) {
26                 stack.push(current);
27                 if (p.val < current.val) {
28                     current = current.left;
29                 } else {
30                     current = current.right;
31                 }
32             }
33             // pop stack
34             TreeNode prev = p;
35             while (!stack.isEmpty()) {
36                 TreeNode cur = stack.pop();
37                 if (cur.left == prev) {
38                     result = cur;
39                     break;
40                 }
41                 prev = cur;
42             }
43         }
44         return result;
45     }
46 }

Solution 2 -- Recursive

Find Successor & Predecessor in BST

时间: 2024-08-06 03:45:51

Inorder Successor in BST 解答的相关文章

[Lintcode] Inorder Successor in BST

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. Example Given tree = [2,1] and node = 1: 2 / 1 return node 2. Given tree = [2,1,3] and node = 2: 2 / 1 3 return node 3. Note If

[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 that node in the BST. Example Given tree = [2,1] and node = 1: 2 / 1 return node 2. Given tree = [2,1,3] and node = 2: 2 / \1 3 return node 3. Note I

*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. Note: If the given node has no in-order successor in the tree, return null. 解法一:俺自个儿的方法,居然跑了16ms...妈蛋! public class Solution { public TreeNode inorderSu

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. Note: If the given node has no in-order successor in the tree, return null. 本题要求按中序遍历节点p的下一个节点是什么.这道题还是很有难度的,虽然递归实现起来比较容易.思路是,先思考,一个节点按中序的话,它的后继节点是什么呢?有

[LeetCode] Inorder Successor in BST

Problem Description: 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. There are just two cases: The easier one: p has right s

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. Note: If the given node has no in-order successor in the tree, return null. 1 /** 2 * Definition for a binary tree node. 3 * public class TreeNode { 4 *

[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. Note: If the given node has no in-order successor in the tree, return null. 给一个二叉搜索树和它的一个节点,找出它的中序后继节点,如果没有返回null. 解法1: 用中序遍历二叉搜索树,当找到root.val = p.val的时

285. Inorder Successor in BST - Medium

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. Example 1: Input: root = [2,1,3], p = 1 2 / 1 3 Output: 2 Example 2: Input:

[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. The successor of a node p is the node with the smallest key greater than p.val. You will have direct access to the node but not to the root of the tree.