Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Populating Next Right Pointers in Each Node Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Populating Next Right Pointers in Each Node

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

A perfect binary tree is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level.

Given a perfect binary tree which contains an extra next pointer in all the node, populate the next pointers of each node to its next right node.

In nodes with no right nodes, set next to null.

Approach

The idea is to perform a level order traversal and for each node in a level set its next pointer to the next node in the same level.

For the last node in a level, set its next pointer to NULL.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
/* This is the Node class definition

class Node {
public:
    Node* left;
    Node* right;
    Node* next;
    int data;

    Node(int data) {
        this->left = NULL;
        this->right = NULL;
        this->next = NULL;
        this->data = data;
    }
};
*/

void connect(Node* root) {
	queue<Node*> treeNodes;
	treeNodes.push(root);
	while (!treeNodes.empty()) {
		int n = treeNodes.size();
		for (int i = 0; i < n; i++) {
			Node* currNode = treeNodes.front();
			treeNodes.pop();
			if (i == n - 1) {
				currNode->next = NULL;
			} else {
				currNode->next = treeNodes.front();
			}
			if (currNode->left != NULL) {
				treeNodes.push(currNode->left);
			}
			if (currNode->right != NULL) {
				treeNodes.push(currNode->right);
			}
		}
	}
	return;
}
Java
/* This is the Node class definition

class Node {
	public Node left;
    public Node right;
    public Node next;
    int data;

    Node(int data) {
        this.data = data;
    }
}
*/

class Solution {
	void connect(Node root) {
    	Queue<Node> treeNodes = new LinkedList<Node> ();
		treeNodes.add(root);
		while (!treeNodes.isEmpty()) {
			int n = treeNodes.size();
			for (int i = 0; i < n; i++) {
				Node currNode = treeNodes.peek();
				treeNodes.poll();
				if (i == n - 1) {
					currNode.next = null;
				} else {
					currNode.next = treeNodes.peek();
				}
				if (currNode.left != null) {
					treeNodes.add(currNode.left);
				}
				if (currNode.right != null) {
					treeNodes.add(currNode.right);
				}
			}
		}
		return;
	}
}

Another Approach

The idea is to perform a level order traversal, but instead of using a queue we will traverse a level with the help of the next pointer of the nodes.

  • Initially set the next pointer of root to NULL.
  • Now use two nested loop, the outer loop to point to the first node of each level and the inner loop for traversing the level.
  • While traversing a level, set the next pointers of the next level. To do that set the next pointer of the left child of a node to its right child (currNode->left->next = currNode->right) and set the next pointer of the right child of a node to the left child of its next node(currNode->right->next = currNode->next->left).

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
/* This is the Node class definition

class Node {
public:
    Node* left;
    Node* right;
    Node* next;
    int data;

    Node(int data) {
        this->left = NULL;
        this->right = NULL;
        this->next = NULL;
        this->data = data;
    }
};
*/

void connect(Node* root) {
	root->next = NULL;
	while (root != NULL) {
		Node* currNode = root;
		while (currNode != NULL) {
			if (currNode->left != NULL) {
				currNode->left->next = currNode->right;
			}
			if(currNode->right != NULL) {
				if (currNode->next != NULL) {
					currNode->right->next = currNode->next->left;
				} else {
					currNode->right->next = NULL;
				}
			}
			currNode = currNode->next;
		}
		root = root->left;
	}
	return;
}
Java
/* This is the Node class definition

class Node {
	public Node left;
    public Node right;
    public Node next;
    int data;

    Node(int data) {
        this.data = data;
    }
}
*/

class Solution {
	void connect(Node root) {
    	root.next = null;
		while (root != null) {
			Node currNode = root;
			while (currNode != null) {
				if (currNode.left != null) {
					currNode.left.next = currNode.right;
				}
				if(currNode.right != null) {
					if (currNode.next != null) {
						currNode.right.next = currNode.next.left;
					} else {
						currNode.right.next = null;
					}
				}
				currNode = currNode.next;
			}
			root = root.left;
		}
		return;
	}
}
Related Content
Balanced Binary Tree
Binary Search Tree (BST) Iterator
Binary Tree to Doubly Linked List
Binary Tree Inorder Traversal
Maximum Path Sum of Binary Tree
Binary Tree Postorder Traversal
Binary Tree Preorder Traversal
Bottom View of Binary Tree
Construct Binary Tree from Inorder and Postorder Traversal
Construct Binary Tree from Preorder and Inorder Traversal
Delete Node in a Binary Search Tree (BST)
Diameter of Binary Tree
Flatten Binary Tree to Linked List
Identical Binary Trees
Inorder Predecessor of Node in BST
Inorder Successor of Node in BST
Insert into a Binary Search Tree (BST)
Invert Binary Tree
Is Binary Tree BST
Kth Largest in BST
Kth Smallest in BST
Left View of Binary Tree
Level Order of Binary Tree
Lowest Common Ancestor in Binary Tree
Lowest Common Ancestor in BST
Maximum Depth of Binary Tree
Right View of Binary Tree
Search in a Binary Search Tree (BST)
Serialize and Deserialize Binary Search Tree (BST)
Size of the Largest BST in a Binary Tree
Symmetric Binary Tree
Top View of Binary Tree
Two Sum in BST
Binary Tree Zigzag Level Order Traversal
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet