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

00:00:00

Given a binary tree, invert the binary tree - Inverting a binary tree means that the resulting binary tree should be a mirror replica of the input binary tree.

```
Binary Tree
1
/ \
2 3
/ \ \
4 5 7
/ \
8 9
```

```
Binary Tree
1
/ \
3 2
/ / \
7 5 4
/ \
9 8
```

The resulting output binary tree is an exact mirror replica of the given input binary tree. Hence, the output is an inverted binary tree.

```
0 <= N <=1000;
N = Node value in a Binary Tree.
```

Click to reveal

Try to swap the two children nodes of the root node. Would you swap the leaf nodes first or would you swap the parents first? Are both ways correct?

Click to reveal

Can you try using recursion and somehow use swapping to solve this problem? What does swap actually mean in the context of left and right subtrees?

Click to reveal

Can you try solving this problem iteratively? Can you use data structures like queues, stacks, and arrays to solve this problem? Which specific data structure can help you traverse the tree in a Breadth-First pattern?

Uploaded by: Manu Arora

Share this problem on