Articles by FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
FavTutor
  • AI News
  • Data Structures
  • Web Developement
  • AI Code GeneratorNEW
  • Student Help
  • Main Website
No Result
View All Result
Articles by FavTutor
No Result
View All Result
Home Data Structures and Algorithms

Populating Next Right Pointers In Each Node (with code)

Mansi Agarwal by Mansi Agarwal
March 27, 2024
Reading Time: 6 mins read
Next Right Pointers In Each Node
Follow us on Google News   Subscribe to our newsletter

A perfect binary tree is where all leaves are on the same level with every parent having two children. In this article, we will discuss the leetcode problem where we will Populate Next Right Pointers In Each Node based on a perfectly balanced binary tree.

Next Right Pointers In Each Node Problem

In this problem, we are given a perfectly balanced binary tree where all the leaf nodes of the tree are at the same level. We have to place the right pointers as the next node in each node. If there is no right node present mark its next node as NULL.

Let’s examine this with an example:

Example

In the above example, orange lines indicate the next pointer to each node.

The basic approach to solving this question is to visit each node find its level and store that node in a map according to its level. After traversing the whole array we will traverse the map according to levels and keep marking the next node with the next value of that level. If it is the last node we will mark its next node NULL.

Let’s examine the approach step by step:

  1. First, create a hash map that will store the nodes of the tree level-wise.
  2. After that create a function that will take the current node and level as variables, now push the value of a node in the hash map according to its level.
  3. After that call the function for the left and the right node by increasing the level by 1.
  4. Once we have visited the whole tree, call the hash map and mark the next node with the next value of that level. If the next value is not present mark its next node NULL.

The time complexity for the above process is O(n) as we are visiting the tree once using DFS. DFS time complexity is O(n) so the overall time complexity is O(n). The space complexity will be O(n) as we are storing the values into the hash map whose time complexity is O(n).

Optimized Solution

In the above approach, we are storing the values into the hash map which is taking O(n) space complexity. To reduce this space complexity to constant space we will do level-wise traversal. Here’s how to do it:

  1. First, create a queue that will store the nodes on a level.
  2. Store the root node in the queue. After pushing the root node push NULL. NULL indicates that this level ends here. After that next level will start.
  3. Now iterate the queue till its size becomes 0. 
  4. Now take out the current node from the queue and store it in a temp variable. Check if the next node present in the queue is NULL or not.
  5. If it is not NULL mark the next of temp as this node If it is NULL mark the next of temp NULL and take out that node from the queue. Push the NULL node in the queue.
  6. After that push the left and the right node in the queue.

Now, it’s time to implement it.

C++ Code

Below is the C++ implementation to populate the next right pointers in each node:

#include<bits/stdc++.h>

using namespace std;

class Node{

    public:

    int val;

    Node* left;

    Node* right;

    Node* next;

    Node(int x){

        val = x;

        left = NULL;

        right = NULL;

        next = NULL;

    }

};

Node* find_next(Node* root){

        if(root == NULL) return root;

        queue<Node*>q;

        q.push(root);

        q.push(NULL);

        while(q.size()){

            Node* temp = q.front();

            if(temp ==NULL){

                q.pop();

                if(q.size()) q.push(NULL);

                continue;

            }

            q.pop();

            if(q.front()){                

                temp->next = q.front();

            }

            if(temp->left) q.push(temp->left);

            if(temp->right) q.push(temp->right);

        }

        return root;

}

int main(){

    Node* root =  new Node(1);

    root->left = new Node(2);

    root->right = new Node(3);

    root->left->left = new Node(4);

    root->left->right = new Node(5);

    root->right->left = new Node(6);

    root->right->right = new Node(7);

    find_next(root);

    return 0;

}

Java Code

Below is the Java program for the solution:

import java.util.LinkedList;

import java.util.Queue;

class Node {

    int val;

    Node left, right, next;

    Node(int x) {

        val = x;

        left = null;

        right = null;

        next = null;

    }

}

class NextPointer {

    static Node findNext(Node root) {

        if (root == null) return root;

        Queue<Node> q = new LinkedList<>();

        q.add(root);

        q.add(null);

        while (!q.isEmpty()) {

            Node temp = q.poll();

            if (temp == null) {

                if (!q.isEmpty()) q.add(null);

                continue;

            }

            if (q.peek() != null) {

                temp.next = q.peek();

            }

            if (temp.left != null) q.add(temp.left);

            if (temp.right != null) q.add(temp.right);

        }

        return root;

    }

    public static void main(String[] args) {

        Node root = new Node(1);

        root.left = new Node(2);

        root.right = new Node(3);

        root.left.left = new Node(4);

        root.left.right = new Node(5);

        root.right.left = new Node(6);

        root.right.right = new Node(7);

        findNext(root);

    }

}

Python Code

We can implement it in Python also:

from queue import Queue

class Node:

    def __init__(self, x):

        self.val = x

        self.left = None

        self.right = None

        self.next = None

def find_next(root):

    if root is None:

        return root

    q = Queue()

    q.put(root)

    q.put(None)

    while not q.empty():

        temp = q.get()

        if temp is None:

            if not q.empty():

                q.put(None)

            continue

        if not q.empty() and q.queue[0] is not None:

            temp.next = q.queue[0]

        if temp.left:

            q.put(temp.left)

        if temp.right:

            q.put(temp.right)

    return root

if __name__ == "__main__":

    root = Node(1)

    root.left = Node(2)

    root.right = Node(3)

    root.left.left = Node(4)

    root.left.right = Node(5)

    root.right.left = Node(6)

    root.right.right = Node(7)

    find_next(root)

The time complexity to populate the next right pointers in each node is O(n). The space complexity is O(1) as we are not using any extra space. Here’s how both the approaches compare and reduce space complexity:

ApproachTime ComplexitySpace ComplexityDescription
Brute ForceO(n)O(n)We are storing the values into the hash map whose space complexity is O(n).
Optimized O(n)O(1)We are doing level-wise traversal which reduced our space complexity to O(1).

Conclusion

In this article, we discussed various methods to populate Next Right Pointers In Each Node problem from leetcode. We had seen that by using hash-map we are taking extra O(n) space which can be reduced if we use level-wise traversal. Now, check how to print the right-side view of a binary tree, which is also as interesting.

ShareTweetShareSendSend
Mansi Agarwal

Mansi Agarwal

I am Mansi Agarwal, a final-year student at IIT BHU. I am very keen on competitive programming, with my primary area of interest being Data Structures & Algorithms. I have solved over 1000 LeetCode problems.

RelatedPosts

validate ip address

Validate IP Address Problem ( C++, JAVA, Python)

April 3, 2025
Shortest Palindrome Problem

Shortest Palindrome Problem ( C++, JAVA, Python)

April 4, 2025
Zigzag Conversion Problem

Zigzag Conversion Problem (C++, Java, Python)

April 4, 2025
Coin Change II

Coin Change II Problem (C++, Java, Python)

April 7, 2025
Minimum Size Subarray Sum

Minimum Size Subarray Sum Problem (C++, Java, Python)

April 7, 2025

About FavTutor

FavTutor is a trusted online tutoring service to connects students with expert tutors to provide guidance on Computer Science subjects like Java, Python, C, C++, SQL, Data Science, Statistics, etc.

Categories

  • AI News, Research & Latest Updates
  • Trending
  • Data Structures
  • Web Developement
  • Data Science

Important Subjects

  • Python Assignment Help
  • C++ Help
  • R Programming Help
  • Java Homework Help
  • Programming Help

Resources

  • About Us
  • Contact Us
  • Editorial Policy
  • Privacy Policy
  • Terms and Conditions

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.

No Result
View All Result
  • AI News
  • Data Structures
  • Web Developement
  • AI Code Generator
  • Student Help
  • Main Website

Website listed on Ecomswap. © Copyright 2025 All Rights Reserved.