Print all the nodes except the leftmost node in every level of the given binary tree
Last Updated :
07 Feb, 2022
Given a binary tree, the task is to print all the nodes except the leftmost in every level of the tree. The root is considered at level 0, and left most node of any level is considered as a node at position 0.
Examples:
Input:
1
/ \
2 3
/ \ \
4 5 6
/ \
7 8
/ \
9 10
Output:
3
5 6
8
10
Input:
1
/ \
2 3
\ \
4 5
Output:
3
5
Approach: To print nodes level by level, use level order traversal. The idea is based on Print level order traversal line by line. For that, traverse nodes level by level and mark leftmost flag true just before the processing of each level and mark it false just after processing of the first node at each level.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node *left, *right;
};
struct Node* newNode( int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
void excludeLeftmost(Node* root)
{
if (root == NULL)
return ;
queue<Node*> q;
q.push(root);
while (1) {
int nodeCount = q.size();
if (nodeCount == 0)
break ;
bool leftmost = true ;
while (nodeCount > 0) {
Node* node = q.front();
if (leftmost)
leftmost = !leftmost;
else
cout << node->data << " " ;
q.pop();
if (node->left != NULL)
q.push(node->left);
if (node->right != NULL)
q.push(node->right);
nodeCount--;
}
cout << "\n" ;
}
}
int main()
{
struct Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
root->left->right->left = newNode(8);
root->left->right->right = newNode(9);
root->left->right->right->right = newNode(10);
excludeLeftmost(root);
return 0;
}
|
Java
import java.util.*;
class Sol
{
static class Node
{
int data;
Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static void excludeLeftmost(Node root)
{
if (root == null )
return ;
Queue<Node> q = new LinkedList<Node>();
q.add(root);
while ( true )
{
int nodeCount = q.size();
if (nodeCount == 0 )
break ;
boolean leftmost = true ;
while (nodeCount > 0 )
{
Node node = q.peek();
if (leftmost)
leftmost = !leftmost;
else
System.out.print( node.data + " " );
q.remove();
if (node.left != null )
q.add(node.left);
if (node.right != null )
q.add(node.right);
nodeCount--;
}
System.out.println();
}
}
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( 5 );
root.right.left = newNode( 6 );
root.right.right = newNode( 7 );
root.left.right.left = newNode( 8 );
root.left.right.right = newNode( 9 );
root.left.right.right.right = newNode( 10 );
excludeLeftmost(root);
}
}
|
Python3
from collections import dequeue
class Node:
def __init__( self ):
self .data = 0
self .left = None
self .right = None
def newNode(data: int ) - > Node:
node = Node()
node.data = data
node.left = None
node.right = None
return node
def excludeLeftMost(root: Node):
if root is None :
return
q = dequeue()
q.append(root)
while 1 :
nodeCount = len (q)
if nodeCount = = 0 :
break
leftmost = True
while nodeCount > 0 :
node = q[ 0 ]
if leftmost:
leftmost = not leftmost
else :
print (node.data, end = " " )
q.popleft()
if node.left is not None :
q.append(node.left)
if node.right is not None :
q.append(node.right)
nodeCount - = 1
print ()
if __name__ = = "__main__" :
root = Node()
root = newNode( 1 )
root.left = newNode( 2 )
root.right = newNode( 3 )
root.left.left = newNode( 4 )
root.left.right = newNode( 5 )
root.right.left = newNode( 6 )
root.right.right = newNode( 7 )
root.left.right.left = newNode( 8 )
root.left.right.right = newNode( 9 )
root.left.right.right.right = newNode( 10 )
excludeLeftMost(root)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
public class Node
{
public int data;
public Node left, right;
};
static Node newNode( int data)
{
Node node = new Node();
node.data = data;
node.left = node.right = null ;
return (node);
}
static void excludeLeftmost(Node root)
{
if (root == null )
return ;
Queue<Node> q = new Queue<Node>();
q.Enqueue(root);
while ( true )
{
int nodeCount = q.Count;
if (nodeCount == 0)
break ;
Boolean leftmost = true ;
while (nodeCount > 0)
{
Node node = q.Peek();
if (leftmost)
leftmost = !leftmost;
else
Console.Write( node.data + " " );
q.Dequeue();
if (node.left != null )
q.Enqueue(node.left);
if (node.right != null )
q.Enqueue(node.right);
nodeCount--;
}
Console.WriteLine();
}
}
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(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.right.left = newNode(8);
root.left.right.right = newNode(9);
root.left.right.right.right = newNode(10);
excludeLeftmost(root);
}
}
|
Javascript
<script>
class Node
{
constructor(data)
{
this .left = null ;
this .right = null ;
this .data = data;
}
}
function newNode(data)
{
let node = new Node(data);
return (node);
}
function excludeLeftmost(root)
{
if (root == null )
return ;
let q = [];
q.push(root);
while ( true )
{
let nodeCount = q.length;
if (nodeCount == 0)
break ;
let leftmost = true ;
while (nodeCount > 0)
{
let node = q[0];
if (leftmost)
leftmost = !leftmost;
else
document.write(node.data + " " );
q.shift();
if (node.left != null )
q.push(node.left);
if (node.right != null )
q.push(node.right);
nodeCount--;
}
document.write( "</br>" );
}
}
let root = newNode(1);
root.left = newNode(2);
root.right = newNode(3);
root.left.left = newNode(4);
root.left.right = newNode(5);
root.right.left = newNode(6);
root.right.right = newNode(7);
root.left.right.left = newNode(8);
root.left.right.right = newNode(9);
root.left.right.right.right = newNode(10);
excludeLeftmost(root);
</script>
|
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...