Search⌘ K
AI Features

Solution: Inorder Successor in BST

Explore how to find the inorder successor of a given node in a binary search tree by applying a depth-first search pattern. Understand the algorithm that leverages BST properties to navigate the tree efficiently, updating potential successors and recognizing when no successor exists, while optimizing for time and space complexity.

Statement

You are given the root node of a binary search tree and a specific node p. Your task is to return the inorder successor of this p node. If there is no inorder successor of the given node, return NULL.

Note: The inorder successor of p is the node with the smallest value greater than p.data in the binary search tree.

Constraints:

  • The tree contains nodes in the range [1,500][1,500].

  • 104 ...