Skip to content
Programmingoneonone
Programmingoneonone
  • Engineering Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
    • 100+ C++ Programs
  • Solutions
    • HackerRank
      • Algorithms Solutions
      • C solutions
      • C++ solutions
      • Java solutions
      • Python solutions
    • Leetcode Solutions
    • HackerEarth Solutions
  • Work with US
Programmingoneonone
Programmingoneonone

Leetcode Populating Next Right Pointers in Each Node problem solution

YASH PAL, 31 July 202419 January 2026

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

Leetcode Populating Next Right Pointers in Each Node 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

Populating Next Right Pointers in Each Node 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 solutions Leetcode Problems Solutions Leetcode

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes