Given a Binary Tree and an integer D, the task is to check if the distance between all pairs of same node values in the Tree is ? D or not. If found to be true, then print Yes. Otherwise, print No.
Input: D = 7
1 / \ 2 3 / \ / \ 4 3 4 4
The repeated value of nodes are 3 and 4.
The distance between the two nodes valued 3, is 3.
The maximum distance between any pair of nodes valued 4 is 4.
Therefore, none of the distances exceed 7
Input: D = 1
3 / \ 3 3 \ 3
The idea is to observe that the problem is similar to finding the distance between two nodes of a tree. But there can be multiple pairs of nodes for which we have to find the distance. Follow the steps below:
- Perform the Post Order Traversal of the given tree and find the distance between the repeated pairs of nodes.
- Find the nodes that are repeated in the tree using unordered_map.
- For each repeated node of a particular value, find the maximum possible distance between any pair.
- If that distance is > D, print “No”.
- If no such node value is found having a pair containing that value, exceeding D, then print “Yes”.
Below is the implementation of the above approach:
Time Complexity: O(N2)
Auxiliary Space: O(1)
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.
- Count pairs of leaf nodes in a Binary Tree which are at most K distance apart
- Minimum Sum of a pair at least K distance apart from an Array
- Least Common Ancestor of any number of nodes in Binary Tree
- Count of 0s to be flipped to make any two adjacent 1s at least K 0s apart
- Distance between two nodes of binary tree with node values from 1 to N
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Common nodes in the inorder sequence of a tree between given two nodes in O(1) space
- Sum of all the child nodes with even parent values in a Binary Tree
- Print all nodes in a binary tree having K leaves
- Print common nodes on path from root (or common ancestors)
- Find distance between two nodes of a Binary Tree
- Queries to find distance between two nodes of a Binary tree
- Queries to find distance between two nodes of a Binary tree - O(logn) method
- Shortest distance between two nodes in an infinite binary tree
- Find distance between two nodes in the given Binary tree for Q queries
- Minimum distance to visit all the nodes of an undirected weighted tree
- Print the nodes of Binary Tree having a grandchild
- Print the nodes having exactly one child in a Binary tree
- Check if all nodes of the Binary Tree can be represented as sum of two primes
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
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.