# Print the nodes that are just above the leaf node

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.

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `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; ` `} `

Output:

```20 8 12
```

Time Complexity: O(N)
Auxiliary Space: O(1) My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.
{"ft_lang_label":"__label__en","ft_lang_prob":0.78139037,"math_prob":0.95512414,"size":4462,"snap":"2020-34-2020-40","text_gpt3_token_len":1189,"char_repetition_ratio":0.18909825,"word_repetition_ratio":0.13436386,"special_character_ratio":0.2902286,"punctuation_ratio":0.08932715,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99543446,"pos_list":[0,1,2,3,4,5,6,7,8],"im_url_duplicate_count":[null,5,null,5,null,null,null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2020-08-04T17:05:10Z\",\"WARC-Record-ID\":\"<urn:uuid:c92f1a86-b3cb-41ef-9e5f-8aec17c06542>\",\"Content-Length\":\"119494\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:f2983468-4369-4067-b3b1-e64d552ab4f4>\",\"WARC-Concurrent-To\":\"<urn:uuid:46eee5e9-5d6f-4cbe-8e5e-1712fdd05bf9>\",\"WARC-IP-Address\":\"23.199.63.80\",\"WARC-Target-URI\":\"https://www.geeksforgeeks.org/print-the-nodes-that-are-just-above-the-leaf-node/\",\"WARC-Payload-Digest\":\"sha1:ABGZLZBZAE4HBNIPGZPJ23W5RYUSOLBH\",\"WARC-Block-Digest\":\"sha1:Q72ADU2EPO3HIKKRCMSKZ27UOW54ZXUD\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2020/CC-MAIN-2020-34/CC-MAIN-2020-34_segments_1596439735881.90_warc_CC-MAIN-20200804161521-20200804191521-00023.warc.gz\"}"}