[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:
- target.val >= root.val: go to root.right.
- target.val < root.val: go to root.left. There're two cases:
- If the function on root.left returns null, that means the root node is the successor of target.
- If the function on root.left returns a node, traverse deeper.
Solution (Recursive):
Solution (Iterative):
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:
What if I need to find the inorder successor in a binary tree? I still use recursion. There are several cases:
- Base case: root == null. Return null.
- If root.val == p.val:
- If root has no right subtree: return root's parent.
- Otherwise, return the left-most node of root's right sub-tree.
- Check if p's inorder successor is in root's left subtree. Pass the current root node as parent.
- If it's not in root's left subtree, check root's right subtree. The parent passed should be root's parent.
Solution:
Time complexity: O(n).
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
Post a Comment