# Print the nodes of binary tree as they become the leaf node

Given a binary tree. First print all leaf nodes, after that remove all the leaf nodes from the tree and now print all the new formed leaf nodes and keep doing this until all the nodes are removed from the tree.

**Examples **:

Input: 8 / \ 3 10 / \ / \ 1 6 14 4 / \ 7 13Output: 4 6 7 13 14 1 10 3 8

**Source** :Flipkart On Campus Recruitment

**Approach** : The idea is to perform simple dfs and assign different values to each node on the basis of following conditions:

- Initially assign all the nodes with value as 0.
- Now, Assign all the nodes with the value as
**(maximum value of both child)+1**.

**Tree before DFS**: A temporary value zero is assigned to all of the nodes.

**Tree after DFS**: Nodes are assigned with the value as **(maximum value of both child)+1**.

Now, you can see in the above tree that after all the values are assigned to each node, the task now reduces to print the tree on the basis of increasing order of node values assigned to them.

Below is the implementation of above approach:

`// C++ program to print the nodes of binary ` `// tree as they become the leaf node ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Binary tree node ` `struct` `Node { ` ` ` `int` `data; ` ` ` `int` `order; ` ` ` `struct` `Node* left; ` ` ` `struct` `Node* right; ` `}; ` ` ` `// Utiltiy function to allocate a new node ` `struct` `Node* newNode(` `int` `data, ` `int` `order) ` `{ ` ` ` `struct` `Node* node = ` `new` `Node; ` ` ` `node->data = data; ` ` ` `node->order = order; ` ` ` `node->left = NULL; ` ` ` `node->right = NULL; ` ` ` ` ` `return` `(node); ` `} ` ` ` `// Function for inorder traversal of tree and ` `// assigning values to nodes ` `void` `Inorder(` `struct` `Node* node, vector<pair<` `int` `, ` `int` `> >& v) ` `{ ` ` ` `if` `(node == NULL) ` ` ` `return` `; ` ` ` ` ` `/* first recur on left child */` ` ` `Inorder(node->left, v); ` ` ` ` ` `/* now recur on right child */` ` ` `Inorder(node->right, v); ` ` ` ` ` `// If current node is leaf node, it's order will be 1 ` ` ` `if` `(node->right == NULL && node->left == NULL) { ` ` ` `node->order = 1; ` ` ` ` ` `// make pair of assigned value and tree value ` ` ` `v.push_back(make_pair(node->order, node->data)); ` ` ` `} ` ` ` `else` `{ ` ` ` `// otherwise, the order will be: ` ` ` `// max(left_child_order, right_child_order) + 1 ` ` ` `node->order = max((node->left)->order, (node->right)->order) + 1; ` ` ` ` ` `// make pair of assigned value and tree value ` ` ` `v.push_back(make_pair(node->order, node->data)); ` ` ` `} ` `} ` ` ` `// Function to print leaf nodes in ` `// the given order ` `void` `printLeafNodes(` `int` `n, vector<pair<` `int` `, ` `int` `> >& v) ` `{ ` ` ` `// Sort the vector in increasing order of ` ` ` `// assigned node values ` ` ` `sort(v.begin(), v.end()); ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(v[i].first == v[i + 1].first) ` ` ` `cout << v[i].second << ` `" "` `; ` ` ` ` ` `else` ` ` `cout << v[i].second << ` `"\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `struct` `Node* root = newNode(8, 0); ` ` ` `root->left = newNode(3, 0); ` ` ` `root->right = newNode(10, 0); ` ` ` `root->left->left = newNode(1, 0); ` ` ` `root->left->right = newNode(6, 0); ` ` ` `root->right->left = newNode(14, 0); ` ` ` `root->right->right = newNode(4, 0); ` ` ` `root->left->left->left = newNode(7, 0); ` ` ` `root->left->left->right = newNode(13, 0); ` ` ` ` ` `int` `n = 9; ` ` ` ` ` `vector<pair<` `int` `, ` `int` `> > v; ` ` ` ` ` `Inorder(root, v); ` ` ` `printLeafNodes(n, v); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4 6 7 13 14 1 10 3 8

**Time Complexity** : O(nlogn)

**Auxiliary Space** : O(n), where n is the number of nodes in the given Binary Tree.

## Recommended Posts:

- Print all leaf nodes of a Binary Tree from left to right
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print all nodes that are at distance k from a leaf node
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Print the longest leaf to leaf path in a Binary tree
- Closest leaf to a given node in Binary Tree
- Sum of all leaf nodes of binary tree
- Deepest left leaf node in a binary tree
- Product of all leaf nodes of binary tree
- Count Non-Leaf nodes in a Binary Tree
- Deepest right leaf node in a binary tree | Iterative approach
- Deepest left leaf node in a binary tree | iterative approach
- Pairwise Swap leaf nodes in a binary tree
- Program to count leaf nodes in a 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.