5.23 Inorder Successor in BST
5.23.1 Problem Metadata
- Platform: LeetCode
- Problem ID: 285
- Difficulty: Medium
- URL: https://leetcode.com/problems/inorder-successor-in-bst/
- Tags:
- Techniques: Binary Search Tree, Tree
5.23.3 Solution - BST Properties
5.23.3.1 Walkthrough
Exploit BST ordering:
- If
phas a right subtree, successor is the leftmost node inp.right. - Otherwise, walk down from root tracking the deepest ancestor whose value is greater than
p.val(first left turn on the search path).
Iteratively compare p.val with root.val: when p.val < root.val, record root as a candidate successor and move left; otherwise move right to search larger values.