Level Order Successor of a node in Binary Tree
Last Updated :
14 Jun, 2022
Given a binary tree and a node in the binary tree, find Levelorder successor of the given node. That is, the node that appears after the given node in the level order traversal of the tree.
Note: The task is not just to print the data of the node, you have to return the complete node from the tree.
Examples:
Consider the following binary tree
20
/ \
10 26
/ \ / \
4 18 24 27
/ \
14 19
/ \
13 15
Levelorder traversal of given tree is:
20, 10, 26, 4, 18, 24, 27, 14, 19, 13, 15
Input : 24
Output : 27
Input : 4
Output : 18
Approach:
- Check if the root is NULL, that is tree is empty. If true then return NULL.
- Check if the given node is root. If true:
- Check if left child of root exists, if true return left child of root.
- Else, check if right child exists, return it.
- If the root is the only node. Return NULL.
- Otherwise, perform Level Order Traversal on the tree using a Queue.
- At every step of the level order traversal, check if the current node matches with the given node.
- If True, stop traversing any further and return the element at top of queue which will be the next node in the level order traversal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
struct Node *left, *right;
int value;
};
struct Node* newNode( int value)
{
Node* temp = new Node;
temp->left = temp->right = NULL;
temp->value = value;
return temp;
}
Node* levelOrderSuccessor(Node* root, Node* key)
{
if (root == NULL)
return NULL;
if (root == key) {
if (root->left)
return root->left;
else if (root->right)
return root->right;
else
return NULL;
}
queue<Node*> q;
q.push(root);
while (!q.empty()) {
Node* nd = q.front();
q.pop();
if (nd->left != NULL) {
q.push(nd->left);
}
if (nd->right != NULL) {
q.push(nd->right);
}
if (nd == key)
break ;
}
return q.front();
}
int main()
{
struct Node* root = newNode(20);
root->left = newNode(10);
root->left->left = newNode(4);
root->left->right = newNode(18);
root->right = newNode(26);
root->right->left = newNode(24);
root->right->right = newNode(27);
root->left->right->left = newNode(14);
root->left->right->left->left = newNode(13);
root->left->right->left->right = newNode(15);
root->left->right->right = newNode(19);
struct Node* key = root->right->left;
struct Node* res = levelOrderSuccessor(root, key);
if (res)
cout << "LevelOrder successor of "
<< key->value << " is " << res->value;
else
cout << "LevelOrder successor of "
<< key->value << " is " << "NULL" ;
return 0;
}
|
Java
import java.util.*;
class GfG {
static class Node {
Node left, right;
int value;
}
static Node newNode( int value)
{
Node temp = new Node();
temp.left = null ;
temp.right = null ;
temp.value = value;
return temp;
}
static Node levelOrderSuccessor(Node root, Node key)
{
if (root == null )
return null ;
if (root == key) {
if (root.left != null )
return root.left;
else if (root.right != null )
return root.right;
else
return null ;
}
Queue<Node> q = new LinkedList<Node> ();
q.add(root);
while (!q.isEmpty()) {
Node nd = q.peek();
q.remove();
if (nd.left != null ) {
q.add(nd.left);
}
if (nd.right != null ) {
q.add(nd.right);
}
if (nd == key)
break ;
}
return q.peek();
}
public static void main(String[] args)
{
Node root = newNode( 20 );
root.left = newNode( 10 );
root.left.left = newNode( 4 );
root.left.right = newNode( 18 );
root.right = newNode( 26 );
root.right.left = newNode( 24 );
root.right.right = newNode( 27 );
root.left.right.left = newNode( 14 );
root.left.right.left.left = newNode( 13 );
root.left.right.left.right = newNode( 15 );
root.left.right.right = newNode( 19 );
Node key = root.right.left;
Node res = levelOrderSuccessor(root, key);
if (res != null )
System.out.println( "LevelOrder successor of "
+key.value + " is " + res.value);
else
System.out.println( "LevelOrder successor of "
+key.value + " is NULL" );
}
}
|
Python3
class Node:
def __init__( self , value):
self .left = None
self .right = None
self .value = value
def levelOrderSuccessor(root, key):
if root = = None :
return None
elif root = = key:
if root.left:
return root.left
elif root.right:
return root.right
else :
return None
q = []
q.append(root)
while len (q) ! = 0 :
nd = q.pop( 0 )
if nd.left ! = None :
q.append(nd.left)
if nd.right ! = None :
q.append(nd.right)
if nd = = key:
break
return q[ 0 ]
if __name__ = = "__main__" :
root = Node( 20 )
root.left = Node( 10 )
root.left.left = Node( 4 )
root.left.right = Node( 18 )
root.right = Node( 26 )
root.right.left = Node( 24 )
root.right.right = Node( 27 )
root.left.right.left = Node( 14 )
root.left.right.left.left = Node( 13 )
root.left.right.left.right = Node( 15 )
root.left.right.right = Node( 19 )
key = root.right.left
res = levelOrderSuccessor(root, key)
if res:
print ( "LevelOrder successor of " +
str (key.value) + " is " +
str (res.value))
else :
print ( "LevelOrder successor of " +
str (key.value) + " is NULL" )
|
C#
using System;
using System.Collections.Generic;
class GfG
{
public class Node
{
public Node left, right;
public int value;
}
static Node newNode( int value)
{
Node temp = new Node();
temp.left = null ;
temp.right = null ;
temp.value = value;
return temp;
}
static Node levelOrderSuccessor(Node root, Node key)
{
if (root == null )
return null ;
if (root == key)
{
if (root.left != null )
return root.left;
else if (root.right != null )
return root.right;
else
return null ;
}
LinkedList<Node> q = new LinkedList<Node> ();
q.AddLast(root);
while (q.Count != 0)
{
Node nd = q.First.Value;
q.RemoveFirst();
if (nd.left != null )
{
q.AddLast(nd.left);
}
if (nd.right != null )
{
q.AddLast(nd.right);
}
if (nd == key)
break ;
}
return q.First.Value;
}
public static void Main(String[] args)
{
Node root = newNode(20);
root.left = newNode(10);
root.left.left = newNode(4);
root.left.right = newNode(18);
root.right = newNode(26);
root.right.left = newNode(24);
root.right.right = newNode(27);
root.left.right.left = newNode(14);
root.left.right.left.left = newNode(13);
root.left.right.left.right = newNode(15);
root.left.right.right = newNode(19);
Node key = root.right.left;
Node res = levelOrderSuccessor(root, key);
if (res != null )
Console.WriteLine( "LevelOrder successor of "
+key.value + " is " + res.value);
else
Console.WriteLine( "LevelOrder successor of "
+key.value + " is NULL" );
}
}
|
Javascript
<script>
class Node
{
constructor(value) {
this .left = null ;
this .right = null ;
this .value = value;
}
}
function newNode(value)
{
let temp = new Node(value);
return temp;
}
function levelOrderSuccessor(root, key)
{
if (root == null )
return null ;
if (root == key) {
if (root.left != null )
return root.left;
else if (root.right != null )
return root.right;
else
return null ;
}
let q = [];
q.push(root);
while (q.length > 0) {
let nd = q[0];
q.shift();
if (nd.left != null ) {
q.push(nd.left);
}
if (nd.right != null ) {
q.push(nd.right);
}
if (nd == key)
break ;
}
return q[0];
}
let root = newNode(20);
root.left = newNode(10);
root.left.left = newNode(4);
root.left.right = newNode(18);
root.right = newNode(26);
root.right.left = newNode(24);
root.right.right = newNode(27);
root.left.right.left = newNode(14);
root.left.right.left.left = newNode(13);
root.left.right.left.right = newNode(15);
root.left.right.right = newNode(19);
let key = root.right.left;
let res = levelOrderSuccessor(root, key);
if (res != null )
document.write( "LevelOrder successor of "
+key.value + " is " + res.value);
else
document.write( "LevelOrder successor of "
+key.value + " is NULL" );
</script>
|
Output:
LevelOrder successor of 24 is 27
Time Complexity: O(N), as we are using a while loop which will traverse N times, where N is the number of nodes in the tree.
Auxiliary Space: O(N), as we are using extra space for the queue, which we are using for the level order traversal.
Share your thoughts in the comments
Please Login to comment...