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

Thought process:
In a BST, an in-order traversal gives the nodes in sorted order. Therefore, a node's in-order successor is the smallest node that's larger than it. The idea is to find the successor recursively by comparing root's value with the target's. There are three cases:
  1. target.val >= root.val: go to root.right.
  2. target.val < root.val: go to root.left. There're two cases:
    1. If the function on root.left returns null, that means the root node is the successor of target.
    2. If the function on root.left returns a node, traverse deeper.

Solution (Recursive):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (root == null) {
            return root;
        }
        
        if (p.val < root.val) {
            TreeNode left = inorderSuccessor(root.left, p);
            return left == null ? root : left;
        }
        return inorderSuccessor(root.right, p);
    }
}

Solution (Iterative):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        if (p == null) {
            return null;
        }
        
        TreeNode successor = null;
        while (root != null) {
            if (p.val < root.val) {
                successor = root;
                root = root.left;
            } else {
                root = root.right;
            }
        }
        return successor;
    }
}

Time complexity: O(n) in the worst case where n = number of nodes.

What if I need to find the inorder successor in a binary tree? I still use recursion. There are several cases:
  1. Base case: root == null. Return null.
  2. If root.val == p.val:
    1. If root has no right subtree: return root's parent.
    2. Otherwise, return the left-most node of root's right sub-tree.
  3. Check if p's inorder successor is in root's left subtree. Pass the current root node as parent.
  4. If it's not in root's left subtree, check root's right subtree. The parent passed should be root's parent.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
        return inorderSuccessor(root, p, null);
    }
    
    private TreeNode inorderSuccessor(TreeNode node, TreeNode p, TreeNode ancestor) {
        if (node == null) {
            return null;
        }
        if (node.val == p.val) {
            if (node.right == null) {
                return ancestor;
            }
            return getLeftLeaf(node.right);
        }
        
        TreeNode left = inorderSuccessor(node.left, p, node);
        return left == null ? inorderSuccessor(node.right, p, ancestor) : left;
    }
    
    private TreeNode getLeftLeaf(TreeNode node) {
        while (node.left != null) {
            node = node.left;
        }
        return node;
    }
}

Time complexity: O(n).

Comments

Popular posts from this blog

[LeetCode] 269. Alien Dictionary

[LeetCode] 714. Best Time to Buy and Sell Stock with Transaction Fee

[LeetCode] 631. Design Excel Sum Formula