Print left and right leaf nodes separately in Binary Tree
Given a binary tree, the task is to print left and right leaf nodes separately.
Examples:
Input:
0
/ \
1 2
/ \
3 4
Output:
Left Leaf Nodes: 3
Right Leaf Nodes: 4 2
Input:
0
\
1
\
2
\
3
Output:
Left Leaf Nodes: None
Right Leaf Nodes: 3
Approach:
- Check if given node is null. If null, then return from the function.
- For each traversal at right and left, send information about the child (left or right child) using the parameter type. Set type = 0 while descending to the left branch and set type = 1 for the right branch.
- Check if it is a leaf node. If the node is a leaf node, then store the leaf node in one of the two vectors of left and right child.
- If node is not a leaf node continue traversal.
- In the case of a single node tree, it will be both a root and a leaf node. This case has to be handled separately.
Below is the implementation of the above approach:
C++
// C++ program for the // above approach #include <bits/stdc++.h> using namespace std; // Structure for // Binary Tree Node struct Node { int data; Node* left; Node* right; Node(int x): data(x), left(NULL), right(NULL) {} }; // Function for // dfs traversal void dfs(Node* root, int type, vector<int>& left_leaf, vector<int>& right_leaf) { // If node is // null, return if (!root) { return; } // If tree consists // of a single node if (!root->left && !root->right) { if (type == -1) { cout << "Tree consists of a single node\n"; } else if (type == 0) { left_leaf.push_back(root->data); } else { right_leaf.push_back(root->data); } return; } // If left child exists, // traverse and set type to 0 if (root->left) { dfs(root->left, 0, left_leaf, right_leaf); } // If right child exists, // traverse and set type to 1 if (root->right) { dfs(root->right, 1, left_leaf, right_leaf); } } // Function to print // the solution void print(vector<int>& left_leaf, vector<int>& right_leaf) { if (left_leaf.size() == 0 && right_leaf.size() == 0) return; // Printing left leaf nodes cout << "Left leaf nodes\n"; for (int x : left_leaf) { cout << x << " "; } cout << '\n'; // Printing right leaf nodes cout << "Right leaf nodes\n"; for (int x : right_leaf) { cout << x << " "; } cout << '\n'; } // Driver code int main() { Node* root = new Node(0); root->left = new Node(1); root->right = new Node(2); root->left->left = new Node(3); root->left->right = new Node(4); vector<int> left_leaf, right_leaf; dfs(root, -1, left_leaf, right_leaf); print(left_leaf, right_leaf); return 0; } |
chevron_right
filter_none
Java
// Java program for the // above approach import java.util.*; class GFG { // Structure for // Binary Tree Node static class Node { int data; Node left; Node right; public Node(int data) { this.data = data; this.left = null; this.right = null; } }; // Function for // dfs traversal static void dfs(Node root, int type, Vector<Integer> left_leaf, Vector<Integer> right_leaf) { // If node is // null, return if (root == null) { return; } // If tree consists // of a single node if (root.left == null && root.right == null) { if (type == -1) { System.out.print("Tree consists of a single node\n"); } else if (type == 0) { left_leaf.add(root.data); } else { right_leaf.add(root.data); } return; } // If left child exists, // traverse and set type to 0 if (root.left != null) { dfs(root.left, 0, left_leaf, right_leaf); } // If right child exists, // traverse and set type to 1 if (root.right != null) { dfs(root.right, 1, left_leaf, right_leaf); } } // Function to print // the solution static void print(Vector<Integer> left_leaf, Vector<Integer> right_leaf) { if (left_leaf.size() == 0 && right_leaf.size() == 0) return; // Printing left leaf nodes System.out.print("Left leaf nodes\n"); for (int x : left_leaf) { System.out.print(x + " "); } System.out.println(); // Printing right leaf nodes System.out.print("Right leaf nodes\n"); for (int x : right_leaf) { System.out.print(x + " "); } System.out.println(); } // Driver code public static void main(String[] args) { Node root = new Node(0); root.left = new Node(1); root.right = new Node(2); root.left.left = new Node(3); root.left.right = new Node(4); Vector<Integer> left_leaf = new Vector<Integer>(), right_leaf = new Vector<Integer>(); dfs(root, -1, left_leaf, right_leaf); print(left_leaf, right_leaf); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
C#
// C# program for the // above approach using System; using System.Collections.Generic; class GFG { // Structure for // Binary Tree Node public class Node { public int data; public Node left; public Node right; public Node(int data) { this.data = data; this.left = null; this.right = null; } }; // Function for // dfs traversal static void dfs(Node root, int type, List<int> left_leaf, List<int> right_leaf) { // If node is // null, return if (root == null) { return; } // If tree consists // of a single node if (root.left == null && root.right == null) { if (type == -1) { Console.Write("Tree consists of a single node\n"); } else if (type == 0) { left_leaf.Add(root.data); } else { right_leaf.Add(root.data); } return; } // If left child exists, // traverse and set type to 0 if (root.left != null) { dfs(root.left, 0, left_leaf, right_leaf); } // If right child exists, // traverse and set type to 1 if (root.right != null) { dfs(root.right, 1, left_leaf, right_leaf); } } // Function to print // the solution static void print(List<int> left_leaf, List<int> right_leaf) { if (left_leaf.Count == 0 && right_leaf.Count == 0) return; // Printing left leaf nodes Console.Write("Left leaf nodes\n"); foreach (int x in left_leaf) { Console.Write(x + " "); } Console.WriteLine(); // Printing right leaf nodes Console.Write("Right leaf nodes\n"); foreach (int x in right_leaf) { Console.Write(x + " "); } Console.WriteLine(); } // Driver code public static void Main(String[] args) { Node root = new Node(0); root.left = new Node(1); root.right = new Node(2); root.left.left = new Node(3); root.left.right = new Node(4); List<int> left_leaf = new List<int>(), right_leaf = new List<int>(); dfs(root, -1, left_leaf, right_leaf); print(left_leaf, right_leaf); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
Output:
Left leaf nodes 3 Right leaf nodes 4 2
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
Recommended Posts:
- Print all leaf nodes of a binary tree from right to left
- Print all leaf nodes of a Binary Tree from left to right
- Print leaf nodes in binary tree from left to right using one stack
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print the nodes of binary tree as they become the leaf node
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print the longest leaf to leaf path in a Binary tree
- Deepest left leaf node in a binary tree
- Print all leaf nodes of an n-ary tree using DFS
- Deepest left leaf node in a binary tree | iterative approach
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Sum of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Maximum sum of non-leaf nodes among all levels of the given 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.
Improved By : princiraj1992

