Given a binary tree consisting of **N** nodes, the task is to print the nodes that are just above the leaf node.

**Examples:**

Input:N = 7, Below is the given Binary Tree:

Output:20 8 12

Explanation:

Node 20 is just above the leaf node 22.

Node 8 is just above the leaf node 4.

Node 12 is just above the leaf nodes 10 and 14.

Input:N = 5, Below is the given Binary Tree:

Output:1 2

Explanation:

Node 1 is just above the leaf node 3.

Node 2 is just above the leaf nodes 4 and 5.

**Approach:** The idea is to traverse the tree and for each node, check if it can be the one which is just above the leaf node. For that, the current node must have children and at least one of the children should be a leaf node. Below are the steps:

- Traverse the tree and check each node.
- If the current node has two children, check if any of them is a root node. If yes, then print the current node.
- If the current node has only left or right child, then check if that left or the right child is a leaf node. If yes, then print the current node.
- Else, continue traversing the tree and move to next node.

Below is the implementation of the above approach:

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Node of tree ` `struct` `node { ` ` ` `int` `data; ` ` ` `struct` `node *left, *right; ` `}; ` ` ` `// Creates and initializes a new ` `// node for the tree ` `struct` `node* newnode(` `int` `data) ` `{ ` ` ` `struct` `node* temp = ` `new` `node(); ` ` ` `temp->data = data; ` ` ` `temp->left = temp->right = NULL; ` ` ` `return` `temp; ` `} ` ` ` `// Prints all nodes which are just ` `// above leaf node ` `void` `cal(` `struct` `node* root) ` `{ ` ` ` ` ` `// If tree is empty ` ` ` `if` `(root == NULL) { ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// If it is a leaf node ` ` ` `if` `(root->left == NULL ` ` ` `&& root->right == NULL) { ` ` ` `return` `; ` ` ` `} ` ` ` ` ` `// For internal nodes ` ` ` `else` `{ ` ` ` ` ` `// If node has two children ` ` ` `if` `(root->left != NULL ` ` ` `&& root->right != NULL) { ` ` ` ` ` `if` `((root->left->left == NULL ` ` ` `&& root->left->right == NULL) ` ` ` `|| (root->right->left == NULL ` ` ` `&& root->right->right == NULL)) { ` ` ` ` ` `cout << root->data << ` `" "` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If node has only left child ` ` ` `if` `(root->left != NULL ` ` ` `&& root->right == NULL) { ` ` ` ` ` `if` `(root->left->left == NULL ` ` ` `&& root->left->right == NULL) { ` ` ` ` ` `cout << root->data << ` `" "` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// If node has only right child ` ` ` `if` `(root->right != NULL ` ` ` `&& root->left == NULL) { ` ` ` ` ` `if` `(root->right->left == NULL ` ` ` `&& root->right->right == NULL) { ` ` ` ` ` `cout << root->data << ` `" "` `; ` ` ` `} ` ` ` `} ` ` ` `} ` ` ` ` ` `// Recursively Call for left ` ` ` `// and right subtree ` ` ` `cal(root->left); ` ` ` `cal(root->right); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Construct a tree ` ` ` `node* root = newnode(20); ` ` ` `root->left = newnode(8); ` ` ` `root->right = newnode(22); ` ` ` `root->left->left = newnode(4); ` ` ` `root->left->right = newnode(12); ` ` ` `root->left->right->left = newnode(10); ` ` ` `root->left->right->right = newnode(14); ` ` ` ` ` `// Function Call ` ` ` `cal(root); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

20 8 12

**Time Complexity:** *O(N)*

**Auxiliary Space:** *O(1)*

## Recommended Posts:

- Print all nodes that are at distance k from a leaf node
- Print the nodes of binary tree as they become the leaf node
- Print Leaf Nodes at a given Level
- Print all leaf nodes of an n-ary tree using DFS
- Sum of nodes on the longest path from root to leaf node
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Print all leaf nodes of a Binary Tree from left to right
- Print all leaf nodes of a binary tree from right to left
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print leaf nodes in binary tree from left to right using one stack
- Print left and right leaf nodes separately in Binary Tree
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Print all nodes at distance k from a given node
- Print the longest leaf to leaf path in a Binary tree
- Print all nodes at distance K from given node: Iterative Approach
- Print node whose each neighboring Tree has all nodes of same color
- Print all nodes except rightmost node of every level of the Binary Tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Queries to find sum of distance of a given node to every leaf node in a Weighted 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.