## Solution

#### Approach #1 Using Recursion(DFS) [Accepted]

If the given depth happens to be equal to 1, we can directly put the whole current tree as a left child of the newly added node. Otherwise, we need to put the new node at appropriate levels.

To do so, we make use of a recursive function insert(val,node,depth,n). Here, refers to the value of the new node to be inserted, refers to the depth of the node currently considered, refers to the node calling the current function for its child subtrees and refers to the height at which the new node needs to be inserted.

For inserting the new node at appropriate level, we can start by making a call to insert with the root node and 1 as the current level. Inside every such call, we check if we've reached one level prior to the level where the new node needs to be inserted.

From this level, we can store the roots of the left and right subtrees of the current node temporarily, and insert the new node as the new left and right subchild of the current node, with the temporarily stored left and right subtrees as the left and right subtrees of the newly inserted left or right subchildren appropriately.

But, if we haven't reached the destined level, we keep on continuing the recursive calling process with the left and right children of the current node respectively. At every such call, we also incrmenet the depth of the current level to reflect the depth change appropriately.

The animation below illustrates the process:

Complexity Analysis

• Time complexity : . A total of nodes of the given tree will be considered.

• Space complexity : . The depth of the recursion tree can go upto in the worst case(skewed tree).

#### Approach #2 Using stack(DFS) [Accepted]

Algorithm

We can do the same task as discussed in the last approach by making use of a as well. But, we need to make use of a new data structure, here, to keep a track of the depth of the current node along with its value.

We start by pushing the root onto the . Then, at every step we do as follows:

• Pop an element from the .

• For every Node popped, check if its depth corresponds to one prior to the depth at which the new node needs to be inserted.

• If yes, insert the new nodes appropriately as in the last approach.

• If no, we push both the left and the right child Node(value+depth) of the current node onto the .

• Continue the popping and pushing process till the becomes empty.

Look at the animation below for a better understanding.

Complexity Analysis

• Time complexity : . A total of nodes of the given tree will be considered.

• Space complexity : . The depth of the can go upto in the worst case(skewed tree).

#### Approach #3 Using queue(BFS) [Accepted]

Algorithm

The idea of traversal in the last approach is similar to Depth First Search. In that case, we need to traverse through all the nodes of the given tree in the order of branches. Firstly we explored one branch to as much depth as possible and then continued with the other ones.

If, instead, we go for Breadth First Search, along with keeping track of the depth of the nodes being considered at any moment during the Breadth First Search, we can stop the search process as soon as all the nodes at the depth have been considered once.

To implement this BFS, we make use of a . We start off by pushing the root node of the given tree at the back of the and with the depth of the current level set as 1. Then, at every step, we do the following:

• Remove an element from the front of the and add all its children to the back of another temporary queue, .

• Keep on adding the elements to the back of the till becomes empty. (Once becomes empty, it indicates that all the nodes at the current level have been considered and now contains all the nodes lying at the next level).

• Reinitialize with its value as . Update the current value of the to reflect the level of nodes currently being considered.

• Repeat the process till we reach the depth .

• On hitting this depth level(), add the new nodes appropriately to all the nodes in the currently, as done in the previous approaches.

The following animation illustrates the process.