📌  相关文章
📜  N元树中具有最大深度的最小值节点

📅  最后修改于: 2021-04-27 21:56:23             🧑  作者: Mango

给定一棵N个节点组成的,任务是从根节点开始查找具有最大深度的节点,将根节点的深度设为零。如果最大深度节点多于1个,则找到具有最小值的那个。

例子:

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.


方法:

  • 这个想法是在树上使用深度优先搜索(DFS),对于每个节点,在我们向下移动树时检查每个节点的高度。
  • 检查它是否是到目前为止的最大值,以及它的高度是否等于最大值,那么它是否是最小值的节点。
  • 如果是,则更新到目前为止的最大高度,并相应地更新节点值。

下面是上述方法的实现:

C++
// C++ implementation of for
// the above problem
 
#include 
using namespace std;
 
#define MAX 100000
 
vector 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;
}


Java
// Java implementation of for
// the above problem
import java.util.*;
 
class GFG{
 
static final int MAX = 100000;
@SuppressWarnings("unchecked")
static Vector[] graph = new Vector[MAX + 1];
 
// To store the height of each node
static int maxHeight, minNode;
 
// Function to perform dfs
static 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
public static void main(String[] args)
{
    // Number of nodes
    int N = 5;
    for(int i = 0; i < graph.length; i++)
        graph[i] = new Vector();
     
    // Edges of the tree
    graph[1].add(2);
    graph[1].add(3);
    graph[2].add(4);
    graph[2].add(5);
    maxHeight = 0;
    minNode = 1;
    dfs(1, 1, 0);
    System.out.print(minNode + "\n");
}
}
 
// This code is contributed by sapnasingh4991


Python3
# Python3 implementation of for
# the above problem
MAX = 100000
  
graph = [[] for i in range(MAX + 1)]
  
# To store the height of each node
maxHeight = 0
minNode = 0
  
# Function to perform dfs
def dfs(node, parent, h):
     
    global minNode, maxHeight
     
    # Store the height of node
    height = h
  
    if (height > maxHeight):
        maxHeight = height
        minNode = node
     
    elif (height == maxHeight and
          minNode > node):
        minNode = node
     
    for to in graph[node]:
        if to == parent:
            continue
         
        dfs(to, node, h + 1)
         
# Driver code
if __name__=="__main__":
     
    # Number of nodes
    N = 5
  
    # Edges of the tree
    graph[1].append(2)
    graph[1].append(3)
    graph[2].append(4)
    graph[2].append(5)
  
    maxHeight = 0
    minNode = 1
  
    dfs(1, 1, 0)
     
    print(minNode)
 
# This code is contributed by rutvik_56


C#
// C# implementation of for
// the above problem
using System;
using System.Collections.Generic;
  
public class GFG{
  
static readonly int MAX = 100000;
static List[] graph = new List[MAX + 1];
  
// To store the height of each node
static int maxHeight, minNode;
  
// Function to perform dfs
static 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;
  
    foreach(int to in graph[node])
    {
        if (to == parent)
            continue;
        dfs(to, node, h + 1);
    }
}
  
// Driver code
public static void Main(String[] args)
{
    for(int i = 0; i < graph.Length; i++)
        graph[i] = new List();
          
    // Edges of the tree
    graph[1].Add(2);
    graph[1].Add(3);
    graph[2].Add(4);
    graph[2].Add(5);
    maxHeight = 0;
    minNode = 1;
    dfs(1, 1, 0);
    Console.Write(minNode + "\n");
}
}
  
// This code is contributed by shikhasingrajput


输出:
4