Register for our webinar

1 hour

Step 1

Step 2

Congratulations!

You have registered for our webinar

Oops! Something went wrong while submitting the form.

Step 1

Step 2

Confirmed

You are scheduled with Interview Kickstart.

Redirecting...

Oops! Something went wrong while submitting the form.

Head of Career Skills Development & Coaching

*Based on past data of successful IK students

Given a binary tree, populate next_right pointers in each node and return the root of the tree.

Definition of next_right node: If we store all the nodes of the same level in an array maintaining the same sequence as in the input tree then (i+1)th node is the next_right node of ith node.

In normal binary tree we have left_ptr and right_ptr but for this question our binary tree node structure will have next_right along with left_ptr and right_ptr which will be null initially and you are supposed to populate that based on the above mentioned definition.

**Example**

Input:

** **

Output:

There are three levels in the input tree. If we write the node values level wise then we get:

Level 1: 100

Level 2: 200, 300

Level 3: 400, 500, 600, 700

In the first level there is only one node. So, next right of node having value 1 is null.

In the second level, there are 2 nodes. Left most node’s next right pointer is the rightmost node.

In the third level, there are 4 nodes. Second node is next right of first node, third node is next right to second node and fourth node is next right of third node.

Input Parameters: There is only one argument named *root* denoting the root of the input tree.

Output: Return the *root* of the tree after populating next_right pointers.

Constraints:

• 0 <= number of nodes <= 100000

• 0 <= values stored in the nodes <= 10^9

We have provided two solutions for this problem. Both are optimal considering time and memory complexity.

In this solution we have followed BFS or level wise traversal approach to populate the next right pointer. I am describing the process step by step below.

- Consider root node is at level 0
- For each level
*i*insert all the nodes of this level in a queue. As root node is in level 0, insert it into a queue - Iterate over all the available nodes of the current level. Second node becomes the next_right of first node, third node becomes the next_right of second node and so on.
- At the same time store the left and right child of current level as nodes of the next level.
- Repeat step 3 and 4 until the queue is empty.

**Time Complexity:**

O(*n*) where *n* denotes the number of nodes in the tree.

As we have to traverse all the nodes of a tree, hence the time complexity is O(*n*).

**Auxiliary Space:**

O(*n*) where *n *is the number of nodes in the input tree.

As we are using a queue to store the nodes of the tree, hence O(*n*) extra space is required.

**Space Complexity:**

O(*n*) where *n *is the number of nodes in the input tree.

As to store given linked list it will take O(*n*) space and auxiliary

space is O(*n*) hence space complexity is O(*n*) + O(*n*) → O(*n*)

In this solution we have followed DFS traversal approach to populate the next right pointer. Left child’s next_right pointer is the right child or next available child of any parent which is situated at the right side of the parent. Right child’s next_right pointer is next available child of any parent which are situated at the right side of the parent

**Time Complexity:**

O(*n*) where *n* denotes the number of nodes in the tree.

As we have to traverse all the nodes of a tree, hence the time complexity is O(*n*).

**Auxiliary Space:**

O(*n*) where *n *is the number of nodes in the input tree for using recursion which implicitly use a stack.

**Space Complexity:**

O(*n*) where *n *is the number of nodes in the input tree.

As to store given linked list it will take O(*n*) space and auxiliary

space is O(*n*) hence space complexity is O(*n*) + O(*n*) → O(*n*)

Note: Input and Output will already be taken care of.

Given a binary tree, populate next_right pointers in each node and return the root of the tree.

Definition of next_right node: If we store all the nodes of the same level in an array maintaining the same sequence as in the input tree then (i+1)th node is the next_right node of ith node.

In normal binary tree we have left_ptr and right_ptr but for this question our binary tree node structure will have next_right along with left_ptr and right_ptr which will be null initially and you are supposed to populate that based on the above mentioned definition.

**Example**

Input:

** **

Output:

There are three levels in the input tree. If we write the node values level wise then we get:

Level 1: 100

Level 2: 200, 300

Level 3: 400, 500, 600, 700

In the first level there is only one node. So, next right of node having value 1 is null.

In the second level, there are 2 nodes. Left most node’s next right pointer is the rightmost node.

In the third level, there are 4 nodes. Second node is next right of first node, third node is next right to second node and fourth node is next right of third node.

Input Parameters: There is only one argument named *root* denoting the root of the input tree.

Output: Return the *root* of the tree after populating next_right pointers.

Constraints:

• 0 <= number of nodes <= 100000

• 0 <= values stored in the nodes <= 10^9

We have provided two solutions for this problem. Both are optimal considering time and memory complexity.

In this solution we have followed BFS or level wise traversal approach to populate the next right pointer. I am describing the process step by step below.

- Consider root node is at level 0
- For each level
*i*insert all the nodes of this level in a queue. As root node is in level 0, insert it into a queue - Iterate over all the available nodes of the current level. Second node becomes the next_right of first node, third node becomes the next_right of second node and so on.
- At the same time store the left and right child of current level as nodes of the next level.
- Repeat step 3 and 4 until the queue is empty.

**Time Complexity:**

O(*n*) where *n* denotes the number of nodes in the tree.

As we have to traverse all the nodes of a tree, hence the time complexity is O(*n*).

**Auxiliary Space:**

O(*n*) where *n *is the number of nodes in the input tree.

As we are using a queue to store the nodes of the tree, hence O(*n*) extra space is required.

**Space Complexity:**

O(*n*) where *n *is the number of nodes in the input tree.

As to store given linked list it will take O(*n*) space and auxiliary

space is O(*n*) hence space complexity is O(*n*) + O(*n*) → O(*n*)

In this solution we have followed DFS traversal approach to populate the next right pointer. Left child’s next_right pointer is the right child or next available child of any parent which is situated at the right side of the parent. Right child’s next_right pointer is next available child of any parent which are situated at the right side of the parent

**Time Complexity:**

O(*n*) where *n* denotes the number of nodes in the tree.

As we have to traverse all the nodes of a tree, hence the time complexity is O(*n*).

**Auxiliary Space:**

O(*n*) where *n *is the number of nodes in the input tree for using recursion which implicitly use a stack.

**Space Complexity:**

O(*n*) where *n *is the number of nodes in the input tree.

As to store given linked list it will take O(*n*) space and auxiliary

space is O(*n*) hence space complexity is O(*n*) + O(*n*) → O(*n*)

**Attend our free webinar to amp up your career and get the salary you deserve.**

Hosted By

Ryan Valles

Founder, Interview Kickstart