Find Level wise positions of given node in a given Binary Tree
Last Updated :
01 Apr, 2022
Given a binary tree and an integer X, the task is to find out all the occurrences of X in the given tree, and print its level and its position from left to right on that level. If X is not found, print -1.
Examples:
Input: X=35
10
/ \
20 30
/ \ / \
40 60 35 50
Output: [(3, 3)]
Explanation: Integer X=35 is present in level 3 and its position is 3 from left.
Input: X= 2
1
/ \
2 3
/ \ / \
4 6 8 2
Output: [(2, 1), (3, 4)]
Explanation: Integer X=2 is present in level 2 and its position is 1 from left also it is present in level 3 and position from left is 4.
Approach: This problem can be solved using level order traversal of binary tree using queue.
- Perform level order traversal of given Binary Tree using Queue.
- Keep track of level count and the count of nodes from left to right during traversal
- Whenever X is encountered during traversal, print or store the level count and position of current occurrence of X.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
struct Node *left, *right;
};
void printLevelandPosition(Node* root, int X)
{
if (root == NULL)
return ;
queue<Node*> q;
q.push(root);
int currLevel = 1, position = 1;
while (q.empty() == false ) {
int size = q.size();
while (size--) {
Node* node = q.front();
if (node->data == X)
cout << "(" << currLevel
<< " " << position
<< "), " ;
q.pop();
position++;
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
}
currLevel++;
position = 1;
}
}
Node* newNode( int data)
{
Node* temp = new Node;
temp->data = data;
temp->left = temp->right = NULL;
return temp;
}
int main()
{
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(2);
int X = 2;
printLevelandPosition(root, X);
return 0;
}
|
Java
import java.util.*;
class Node {
int data;
Node left;
Node right;
}
class GFG
{
public static void printLevelandPosition(Node root,
int X)
{
if (root == null )
return ;
Queue<Node> q = new LinkedList<>();
q.add(root);
int currLevel = 1 , position = 1 ;
while (q.size() != 0 ) {
int size = q.size();
while ((size--) != 0 ) {
Node node = q.peek();
if (node.data == X)
System.out.print( "(" + currLevel + " "
+ position + "), " );
q.remove();
position++;
if (node.left != null )
q.add(node.left);
if (node.right != null )
q.add(node.right);
}
currLevel++;
position = 1 ;
}
}
public static Node newNode( int data)
{
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void main(String[] args)
{
Node root = newNode( 1 );
root.left = newNode( 2 );
root.right = newNode( 3 );
root.left.left = newNode( 4 );
root.left.right = newNode( 2 );
int X = 2 ;
printLevelandPosition(root, X);
}
}
|
Python3
class Node:
def __init__( self ,d):
self .data = d
self .left = None
self .right = None
def printLevelandPosition(root, X):
if (root = = None ):
return
q = []
q.append(root)
currLevel,position = 1 , 1
while ( len (q) ! = 0 ):
size = len (q)
while (size ! = 0 ):
node = q[ 0 ]
q = q[ 1 :]
if (node.data = = X):
print (f "({currLevel} {position})," ,end = " " )
position + = 1
if (node.left ! = None ):
q.append(node.left)
if (node.right ! = None ):
q.append(node.right)
size - = 1
currLevel + = 1
position = 1
root = Node( 1 )
root.left = Node( 2 )
root.right = Node( 3 )
root.left.left = Node( 4 )
root.left.right = Node( 2 )
X = 2
printLevelandPosition(root, X)
|
C#
using System;
using System.Collections.Generic;
public class Node {
public int data;
public Node left;
public Node right;
}
public class GFG {
public static void printLevelandPosition(Node root, int X) {
if (root == null )
return ;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
int currLevel = 1, position = 1;
while (q.Count != 0) {
int size = q.Count;
while ((size--) != 0) {
Node node = q.Peek();
if (node.data == X)
Console.Write( "(" + currLevel + " " + position + "), " );
q.Dequeue();
position++;
if (node.left != null )
q.Enqueue(node.left);
if (node.right != null )
q.Enqueue(node.right);
}
currLevel++;
position = 1;
}
}
public static Node newNode( int data) {
Node temp = new Node();
temp.data = data;
temp.left = temp.right = null ;
return temp;
}
public static void Main(String[] args) {
Node root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(2);
int X = 2;
printLevelandPosition(root, X);
}
}
|
Javascript
<script>
class Node {
constructor(d) {
this .data = d;
this .left = null ;
this .right = null ;
}
}
function printLevelandPosition(root, X)
{
if (root == null )
return ;
let q = [];
q.push(root);
let currLevel = 1, position = 1;
while (q.length != 0) {
let size = q.length;
while (size-- != 0) {
let node = q.shift();
if (node.data == X)
document.write( "(" + currLevel
+ " " + position
+ "), " );
position++;
if (node.left != null )
q.push(node.left);
if (node.right != null )
q.push(node.right);
}
currLevel++;
position = 1;
}
}
let root = new Node(1);
root.left = new Node(2);
root.right = new Node(3);
root.left.left = new Node(4);
root.left.right = new Node(2);
let X = 2;
printLevelandPosition(root, X);
</script>
|
Time Complexity: O(n)
Auxiliary Space : O(n)
Share your thoughts in the comments
Please Login to comment...