Given a Binary Tree and an integer K, the task is to check if the Tree consists of a pair of leaf nodes with sum exactly K. In case of multiple pairs, print any one of them. Otherwise print -1.
Note: Assume that the given binary tree will always have more than 1 leaf node.
Input: X = 131 / \ 2 3 / \ / \ 4 5 6 7 \ 8
Output: [5, 8]
The given binary tree consists of 4 leaf nodes [4, 5, 6, 8].
The pair of nodes valued 5 and 8 have sum 13.
Input: X = 6-1 / \ 2 3 / \ 4 5
The given binary tree consists of 3 leaf nodes [4, 5, 3].
No valid pair of nodes exists whose sum of their values equal to 6.
Therefore, print -1.
Naive Approach: The simplest approach to solve the problem is to traverse the tree and store all the leaf nodes in an array. Then sort the array and use two pointer technique to find if a required pair exists or not.
Time Complexity: O(NlogN)
Auxiliary Space: O(N)
Efficient Approach: The above approach can be optimized using HashSet. Follow the steps below to solve the problem:
- Create a Set to store values of leaf nodes.
- Traverse the tree and for every leaf node, check if (K – value of leaf node) exists in the unordered set or not.
- If found to be true, print the oair of node values.
- Otherwise store the value of the current node into the unordered set.
Below is the implementation of the above approach:
Time Complexity: O(N)
Auxiliary Space: O(N)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print the longest leaf to leaf path in a Binary tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Generate Complete Binary Tree in such a way that sum of non-leaf nodes is minimum
- Sum of all leaf nodes of binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Remove nodes from Binary Tree such that sum of all remaining root-to-leaf paths is atleast K
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Program to count leaf nodes in a binary tree
- Pairwise Swap leaf nodes in a binary tree
- Count Non-Leaf nodes in a Binary Tree
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Product of all leaf nodes of binary tree
- Print the nodes of binary tree as they become the leaf node
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print leaf nodes in binary tree from left to right using one stack
- Print all leaf nodes of a binary tree from right to left
- Print left and right leaf nodes separately in Binary Tree
- Find K smallest leaf nodes from a 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 firstname.lastname@example.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.