Basic example
Input
root = TreeNode(20, TreeNode(10, TreeNode(5), TreeNode(15)), TreeNode(30)); p_val = 15
Output
20
The inorder traversal is [5,10,15,20,30]; successor of 15 is 20.
Full lesson preview
Given the root of a Binary Search Tree (BST) and a target value p, find the inorder successor of the node with value p.
Problem statement
Task
Examples
Input
root = TreeNode(20, TreeNode(10, TreeNode(5), TreeNode(15)), TreeNode(30)); p_val = 15
Output
20
The inorder traversal is [5,10,15,20,30]; successor of 15 is 20.
Input format
Output format
Constraints
Samples
Input
TreeNode(20, TreeNode(10, TreeNode(5), TreeNode(15)), TreeNode(30)), 10
Output
15
Successor of 10 is 15.