Print all the nodes except the leftmost node in every level of the given binary tree
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++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Structure of the tree node struct Node { int data; Node *left, *right; }; // Utility method to create a node struct Node* newNode(int data) { struct Node* node = new Node; node->data = data; node->left = node->right = NULL; return (node); } // Function to print all the nodes // except the leftmost in every level // of the given binary tree // with level order traversal void excludeLeftmost(Node* root) { // Base Case if (root == NULL) return; // Create an empty queue for level // order traversal queue<Node*> q; // Enqueue root q.push(root); while (1) { // nodeCount (queue size) indicates // number of nodes at current level. int nodeCount = q.size(); if (nodeCount == 0) break; // Initialize leftmost as true // just before the beginning // of each level bool leftmost = true; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { Node* node = q.front(); // Switch leftmost flag after processing // the leftmost node if (leftmost) leftmost = !leftmost; // Print all the nodes except 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"; } } // Driver code 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
// Java implementation of the approach import java.util.*; class Sol { // Structure of the tree node static class Node { int data; Node left, right; }; // Utility method to create a node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = node.right = null; return (node); } // Function to print all the nodes // except the leftmost in every level // of the given binary tree // with level order traversal static void excludeLeftmost(Node root) { // Base Case if (root == null) return; // Create an empty queue for level // order traversal Queue<Node> q = new LinkedList<Node>(); // Enqueue root q.add(root); while (true) { // nodeCount (queue size) indicates // number of nodes at current level. int nodeCount = q.size(); if (nodeCount == 0) break; // Initialize leftmost as true // just before the beginning // of each level boolean leftmost = true; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { Node node = q.peek(); // Switch leftmost flag after processing // the leftmost node if (leftmost) leftmost = !leftmost; // Print all the nodes except 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(); } } // Driver code 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); } } // This code is contributed by Arnab Kundu |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; class GFG { // Structure of the tree node public class Node { public int data; public Node left, right; }; // Utility method to create a node static Node newNode(int data) { Node node = new Node(); node.data = data; node.left = node.right = null; return (node); } // Function to print all the nodes // except the leftmost in every level // of the given binary tree // with level order traversal static void excludeLeftmost(Node root) { // Base Case if (root == null) return; // Create an empty queue for level // order traversal Queue<Node> q = new Queue<Node>(); // Enqueue root q.Enqueue(root); while (true) { // nodeCount (queue size) indicates // number of nodes at current level. int nodeCount = q.Count; if (nodeCount == 0) break; // Initialize leftmost as true // just before the beginning // of each level Boolean leftmost = true; // Dequeue all nodes of current level // and Enqueue all nodes of next level while (nodeCount > 0) { Node node = q.Peek(); // Switch leftmost flag after processing // the leftmost node if (leftmost) leftmost = !leftmost; // Print all the nodes except 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(); } } // Driver code 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); } } // This code is contributed by PrinciRaj1992 |
3 5 6 7 9
Recommended Posts:
- Print leftmost and rightmost nodes of a Binary Tree
- Print nodes between two given level numbers of a binary tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Print even positioned nodes of even levels in level order of the given binary tree
- Print the nodes of binary tree as they become the leaf node
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Difference between sums of odd level and even level nodes of a Binary Tree
- Get Level of a node in a Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Get level of a node in binary tree | iterative approach
- Depth of the deepest odd level node in Binary Tree
- Level Order Predecessor of a node in Binary Tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



