Given the root of a binary tree and a node in it, return the in-order successor of that node in the BST. If the given node has no in-order successor in the tree, return null.
The successor
here means that the node will be traversed next in an inorder manner for the given node value.
Binary Tree
Target Node = 5
Output = 1
The inorder traversal
of the given binary tree is
Inorder Traversal = 6, 4, 2, 5, 1, 3
The successor of the node with value 5
is the node with value = 1
. Hence, We return the node with value 1
.
-105 <= Node.val <= 105
Click to reveal
Can you try the brute force approach of storing the inorder traversal? How would you find the inorder successor using the leftSubtree->print value->rightSubtree
approach?
Click to reveal
Can you find the leftmost and rightmost values of the target node to conclude some results?