# Minimum valued node having maximum depth in an N-ary Tree

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.

**Examples:**

Input:1 / \ 2 3 / \ 4 5Output:4Explanation: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 / 3Output: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.

**Approach:**

- 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 height equal to 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:

`// C++ implementation of for ` `// the above problem ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `#define MAX 100000 ` ` ` `vector<` `int` `> graph[MAX + 1]; ` ` ` `// To store the height of each node ` `int` `maxHeight, minNode; ` ` ` `// Function to perform dfs ` `void` `dfs(` `int` `node, ` `int` `parent, ` ` ` `int` `h) ` `{ ` ` ` `// Store the height of node ` ` ` `int` `height = h; ` ` ` ` ` `if` `(height > maxHeight) { ` ` ` `maxHeight = height; ` ` ` `minNode = node; ` ` ` `} ` ` ` `else` `if` `(height == maxHeight ` ` ` `&& minNode > node) ` ` ` `minNode = node; ` ` ` ` ` `for` `(` `int` `to : graph[node]) { ` ` ` `if` `(to == parent) ` ` ` `continue` `; ` ` ` `dfs(to, node, h + 1); ` ` ` `} ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Number of nodes ` ` ` `int` `N = 5; ` ` ` ` ` `// Edges of the tree ` ` ` `graph[1].push_back(2); ` ` ` `graph[1].push_back(3); ` ` ` `graph[2].push_back(4); ` ` ` `graph[2].push_back(5); ` ` ` ` ` `maxHeight = 0; ` ` ` `minNode = 1; ` ` ` ` ` `dfs(1, 1, 0); ` ` ` ` ` `cout << minNode << ` `"\n"` `; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

4

## Recommended Posts:

- Replace node with depth in a binary tree
- Depth of the deepest odd level node in Binary Tree
- Minimum and maximum node that lies in the path connecting two nodes in a Binary Tree
- Find Minimum Depth of a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree
- Sum of nodes at maximum depth of a Binary Tree | Set 2
- Write a Program to Find the Maximum Depth or Height of a Tree
- Sum of nodes at maximum depth of a Binary Tree | Iterative Approach
- Node having maximum sum of immediate children and itself in n-ary tree
- Maximum decimal equivalent possible among all connected components of a Binary Valued Graph
- Find the maximum Even Digit Sum node in the given tree
- Maximum XOR with given value in the path from root to given node in the tree
- Get maximum left node in binary tree
- Find the node with minimum value in a Binary Search Tree
- Find the maximum node at a given level in a 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 contribute@geeksforgeeks.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.