Given a Binary Tree where each node has the following structure, write a function to populate the next pointer for all nodes. The next pointer for every node should be set to point to in-order successor.
C++
class node {
public :
int data;
node* left;
node* right;
node* next;
};
|
C
struct node {
int data;
struct node* left;
struct node* right;
struct node* next;
}
|
Java
class Node {
int data;
Node left, right, next;
Node( int item)
{
data = item;
left = right = next = null ;
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
self . next = None
|
C#
class Node {
public int data;
public Node left, right, next;
public Node( int item)
{
data = item;
left = right = next = null ;
}
}
Node root;
|
Javascript
<script>
class Node
{
constructor(x) {
this .data = x;
this .left = null ;
this .right = null ;
}
}
</script>
|
Initially, all next pointers have NULL values. Your function should fill these next pointers so that they point to inorder successor.
Solution (Use Reverse Inorder Traversal)
Traverse the given tree in reverse inorder traversal and keep track of previously visited node. When a node is being visited, assign a previously visited node as next.
C++
#include <bits/stdc++.h>
using namespace std;
class node {
public :
int data;
node* left;
node* right;
node* next;
};
void populateNext(node* p)
{
static node* next = NULL;
if (p) {
populateNext(p->right);
p->next = next;
next = p;
populateNext(p->left);
}
}
node* newnode( int data)
{
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
Node->next = NULL;
return (Node);
}
int main()
{
node* root = newnode(10);
root->left = newnode(8);
root->right = newnode(12);
root->left->left = newnode(3);
populateNext(root);
node* ptr = root->left->left;
while (ptr) {
cout << "Next of " << ptr->data << " is "
<< (ptr->next ? ptr->next->data : -1) << endl;
ptr = ptr->next;
}
return 0;
}
|
Java
class Node {
int data;
Node left, right, next;
Node( int item)
{
data = item;
left = right = next = null ;
}
}
class BinaryTree {
Node root;
static Node next = null ;
void populateNext(Node node)
{
if (node != null ) {
populateNext(node.right);
node.next = next;
next = node;
populateNext(node.left);
}
}
public static void main(String args[])
{
BinaryTree tree = new BinaryTree();
tree.root = new Node( 10 );
tree.root.left = new Node( 8 );
tree.root.right = new Node( 12 );
tree.root.left.left = new Node( 3 );
tree.populateNext(tree.root);
Node ptr = tree.root.left.left;
while (ptr != null ) {
int print
= ptr.next != null ? ptr.next.data : - 1 ;
System.out.println( "Next of " + ptr.data
+ " is: " + print);
ptr = ptr.next;
}
}
}
|
Python3
class Node:
def __init__( self , data):
self .data = data
self .left = None
self .right = None
self . next = None
next = None
def populateNext(p):
global next
if (p ! = None ):
populateNext(p.right)
p. next = next
next = p
populateNext(p.left)
def newnode(data):
node = Node( 0 )
node.data = data
node.left = None
node.right = None
node. next = None
return (node)
root = newnode( 10 )
root.left = newnode( 8 )
root.right = newnode( 12 )
root.left.left = newnode( 3 )
p = populateNext(root)
ptr = root.left.left
while (ptr ! = None ):
out = 0
if (ptr. next ! = None ):
out = ptr. next .data
else :
out = - 1
print ( "Next of" , ptr.data, "is" , out)
ptr = ptr. next
|
C#
using System;
class BinaryTree {
class Node {
public int data;
public Node left, right, next;
public Node( int item)
{
data = item;
left = right = next = null ;
}
}
Node root;
static Node next = null ;
void populateNext(Node node)
{
if (node != null ) {
populateNext(node.right);
node.next = next;
next = node;
populateNext(node.left);
}
}
static public void Main(String[] args)
{
BinaryTree tree = new BinaryTree();
tree.root = new Node(10);
tree.root.left = new Node(8);
tree.root.right = new Node(12);
tree.root.left.left = new Node(3);
tree.populateNext(tree.root);
Node ptr = tree.root.left.left;
while (ptr != null ) {
int print
= ptr.next != null ? ptr.next.data : -1;
Console.WriteLine( "Next of " + ptr.data
+ " is: " + print);
ptr = ptr.next;
}
}
}
|
Javascript
<script>
class Node
{
constructor(x) {
this .data = x;
this .left = null ;
this .right = null ;
}
}
let root;
let next = null ;
function populateNext(node)
{
if (node != null )
{
populateNext(node.right);
node.next = next;
next = node;
populateNext(node.left);
}
}
root = new Node(10)
root.left = new Node(8)
root.right = new Node(12)
root.left.left = new Node(3)
p = populateNext(root)
ptr = root.left.left
while (ptr != null )
{
let print = ptr.next != null ? ptr.next.data : -1;
document.write( "Next of " + ptr.data + " is: " + print+ "<br>" );
ptr = ptr.next;
}
</script>
|
Output
Next of 3 is 8
Next of 8 is 10
Next of 10 is 12
Next of 12 is -1
Time Complexity: O(n)
Auxiliary Space : O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Dec, 2022
Like Article
Save Article