Practice the most popular front-end questions asked in coding interviews with Frontend Churn

00:00:00

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`

.

- The number of nodes in the tree is in the range [1, 104].
`-105 <= Node.val <= 105`

- All Nodes will have unique values.

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?

Uploaded by: Manu Arora

Share this problem on