Input: K = 3
1 / \ 2 3 / 4
The leaf nodes of the tree are 3 and 4
And the shortest distance between them is 3.
This is the only valid pair.
Input: K = 3
1 / \ 2 3 / \ / \ 4 5 6 7
Approach: The idea is to use post order traversal with an array of size K + 1 to keep track of the number of nodes with a particular distance. Below are the steps:
- Initialize an array arr of size K + 1, where arr[i] denotes the number of leaf nodes at distance i from the current node.
- Then, recursively update the above array for the left and the right subtree of every node in an array left and right respectively.
- After the above step, for each node the distance between left and right at the corresponding index will give the distance between the leftmost and rightmost leaf node. Update it in the array res as:
res[i + 1] = left[i] * right[i]
- Now for all possible pairs (l, r) in the arrays left and right, if the sum of the distance between them is at most K, then increment the count of pairs as:
count += left[l] * right[r]
Below is the implementation of the above approach:
Time Complexity: O(N + E), where N is the number of nodes in the Binary tree, and E is the number of Edges.Auxiliary Space: O(H), where H is the height of the Binary tree.
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.
- Check if all the Nodes in a Binary Tree having common values are at least D distance apart
- Minimum Sum of a pair at least K distance apart from an Array
- Print the longest leaf to leaf path in a Binary tree
- Construct XOR tree by Given leaf nodes of Perfect Binary Tree
- Count of Root to Leaf Paths consisting of at most M consecutive Nodes having value K
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Program to count leaf nodes in a binary tree
- Iterative program to count leaf nodes in a Binary Tree
- Count Non-Leaf nodes in a Binary Tree
- Count root to leaf paths having exactly K distinct nodes in a Binary Tree
- Print all nodes that are at distance k from a leaf node
- Remove all leaf nodes from a Generic Tree or N-ary Tree
- Find height of a special binary tree whose leaf nodes are connected
- Sum of all leaf nodes of binary tree
- Pairwise Swap 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 Sum and Product of all Non-Leaf nodes in 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 )
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.