Given a Binary Tree, the task is to find the node from the given tree which has the maximum number of nodes in its subtree with values less than the value of that node. In the case of multiple possible nodes with the same number of maximum nodes, then return any such node.
/ \ / \
2 3 7 14
Node with value 6 has the maximum of nodes which are less than 6 in the subtree of 6 as (2, 3, 5) i.e., 3.
Node with value 21 has the maximum of nodes which are less than 21 in the subtree of 21 as (2, 4, 11) i.e., 3.
Approach: The idea is to use the Post Order traversal. Below are the steps:
- Perform the Post Order Traversal on the given tree.
- Compare the nodes from the left sub-tree and the right sub-tree to its root value and if it is less than the root’s value and store the nodes which are less than the root node.
- Using the above step at each Node, find the number of nodes, then choose the node that has the maximum number of nodes whose keys are less than the current node.
- After the above traversal print that node having the maximum count of lesser node value than that nodes.
Below is the implementation of the above approach:
Time Complexity: O(N2)
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.
- Queries for the number of nodes having values less than V in the subtree of a Node
- Minimum number of Nodes to be removed such that no subtree has more than K nodes
- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Count of nodes having odd divisors in the given subtree for Q queries
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Check if two nodes are in same subtree of the root node
- Queries to find the Minimum Weight from a Subtree of atmost D-distant Nodes from Node X
- Maximum sum subarray having sum less than or equal to given sum using Set
- Find largest subtree having identical left and right subtrees
- Subtree of all nodes in a tree using DFS
- Count of subsets having sum of min and max element less than K
- Find all possible subarrays having product less than or equal to K
- Largest subset having with sum less than equal to sum of respective indices
- Queries for greater than and not less than
- Sum of all array elements less than X and greater than Y for Q queries
- Queries for M-th node in the DFS of subtree
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Find GCD of each subtree of a given node in an N-ary Tree for Q queries
- Maximum size of square such that all submatrices of that size have sum less than K
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.