Given a tree of N nodes, the task is to find the node having maximum depth starting from the root node, taking the root node at zero depth. If there are more than 1 maximum depth node, then find the one having the smallest value.
Input: 1 / \ 2 3 / \ 4 5 Output: 4 Explanation: For this tree: Height of Node 1 - 0, Height of Node 2 - 1, Height of Node 3 - 1, Height of Node 4 - 2, Height of Node 5 - 2. Hence, the nodes whose height is maximum are 4 and 5, out of which 4 is minimum valued. Input: 1 / 2 / 3 Output: 3 Explanation: For this tree: Height of Node 1 - 0, Height of Node 2 - 1, Height of Node 3 - 2 Hence, the node whose height is maximum is 3.
- The idea is to use Depth First Search(DFS) on the tree and for every node, check the height of every node as we move down the tree.
- Check if it is the maximum so far or not and if it has a height equal to the maximum value, then is it the minimum valued node or not.
- If yes then update the maximum height so far and the node value accordingly.
Below is the implementation of the above approach:
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Difference between sum of even and odd valued nodes in a Binary Tree
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Depth of the deepest odd level node in Binary Tree
- Replace node with depth in a binary tree
- Replace every node with depth in N-ary Generic Tree
- Write a Program to Find the Maximum Depth or Height of a Tree
- Sum of nodes at maximum depth of a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Find Minimum Depth of a Binary Tree
- Find Count of Single Valued Subtrees
- Hexadecimal equivalents in Binary Valued Graph
- Octal equivalents of connected components in Binary valued graph
- Find depth of the deepest odd level leaf node
- Calculate depth of a full Binary tree from Preorder
- Depth of an N-Ary tree
- Sum of minimum element at each depth of a given non cyclic graph
- Node having maximum sum of immediate children and itself in n-ary tree
- Minimum cost path from source node to destination node via an intermediate node
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.