Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

Leetcode Populating Next Right Pointers in Each Node problem solution

YASH PAL, 31 July 2024

In this Leetcode Populating Next Right Pointers in Each Node problem solution we have given a perfect binary tree where all leaves are on the same level, and every parent has two children. The binary tree has the following definition:

struct Node {

  int val;

  Node *left;

  Node *right;

  Node *next;

}

Populate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL. Initially, all next pointers are set to NULL.

Leetcode Populating Next Right Pointers in Each Node problem solution

Problem solution in Python.

if root and root.left:
            root.left.next = root.right
            root.right.next = root.next.left if root.next else None
            self.connect(root.left)
            self.connect(root.right)
        return root

Problem solution in Java.

public void connect(TreeLinkNode root) {
        dfs(root);
    }
    private void dfs(TreeLinkNode root){
        if(root == null ||(root.left == null && root.right == null)) return;
        root.left.next = root.right;
        dfs(root.left);
        if(root.next != null) root.right.next = root.next.left;
        dfs(root.right);
    }

Problem solution in C++.

class Solution {
public:
    Node* connect(Node* root) {
        if(root == NULL){
            return root;
        }
        
        Node* leftmost = root;
        while(leftmost->left!=NULL){
            Node* cur = leftmost;
            Node* prev = NULL;
            while(cur != NULL){
                cur->left->next = cur->right;
                if(prev != NULL){
                    prev->right->next = cur->left;
                }
                prev = cur;
                cur = cur->next;
            }
            leftmost = leftmost->left;
        }
        return root;
    }
};

Problem solution in C.

void connect(struct TreeLinkNode *root) {
    struct TreeLinkNode *p = root, *q;
    
    if (!root)
        return;
    
    while (p) {
        q = p;
        while (q) {
            if (!q->left)
                return;
                
            q->left->next = q->right;
            q->right->next = q->next ? q->next->left : NULL;
            
            q = q->next;
        }
        p = p->left;
    }
}

coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
How to download udemy paid courses for free

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
©2025 Programming101 | WordPress Theme by SuperbThemes