当前位置: 首页 > 知识库问答 >
问题:

树中两个节点之间的最大距离

卢杰
2023-03-14

我试图找到树中两个节点之间的最大距离。这是我的程序:

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;


public class distance {

    static ArrayList<ArrayList<Integer>> list=new ArrayList<ArrayList<Integer>>();
    static ArrayList<Integer> visited=new ArrayList<Integer>();
    static ArrayList<Integer> depth=new ArrayList<Integer>();
    static ArrayList<Integer> depth1=new ArrayList<Integer>();
    static int sum=-1;
    static boolean[] arr;
    static int root=0;


    public static void main(String args[])
    {
        Scanner in=new Scanner(System.in);
        int n=in.nextInt();     //no of nodes

        for(int i=0;i<=n;i++)   
        {
            list.add(new ArrayList<Integer>());
        }
        int a;
        int b;
        for(int i=0;i<n-1;i++)  //populating the adjacency list
        {
            a=in.nextInt();
            b=in.nextInt();
            list.get(a).add(b);
            list.get(b).add(a);
        }

        arr=new boolean[n+1];

        dfs(root);

        int final_sum=0;
        Collections.sort(depth1);
        System.out.println(depth1.get(depth1.size()-1)+depth1.get(depth1.size()-2));

        }
    public static void dfs(int n)
    {   
        arr[n]=true;
        visited.add(n);
        sum=sum+1;

        if(list.get(n).size()==1)
        {   
            depth.add(sum); //add the depth to the arraylist when we reach a leaf node.Note the this will not run if the root node has only one child but I've ignored the case.
        }
        if(n==root)
        {
            for(int j=0;j<list.get(0).size();j++)
            {   
                dfs(list.get(0).get(j)); //recursion on each child of the root node
                sum=0;
                Collections.sort(depth);
                depth1.add(depth.get(depth.size()-1));
                depth.clear();
            }

        }
        else
        {
            for(int l:list.get(n))
            if(!arr[l]==true)
            {
                dfs(l);
                sum--;

            }
        }


    }

}

程序似乎正在运行;但是没有为一些测试用例显示正确的输出。我采取的方法是:

  1. 求根节点的子节点数(我一直认为根节点为0)。
  2. 找到每个子树的最大深度(尽可能多的子树,因为有根节点的子节点)。
  3. 将每个子树的最大深度存储在ArrayList中,对其进行排序并打印最后两个值的总和。

有人能指出我程序中的错误吗?

共有1个答案

白昊乾
2023-03-14

错误首先在于算法。假设两个节点之间的最大距离始终包含根节点,但这并不总是成立。

下面是一个例子:

最长路径中的节点用红色标记。最长路径的长度为6,包含7个节点,而您的算法只找到穿过根的路径,因此打印5作为其答案。

 类似资料:
  • 我被这个问题的修改版本困住了(在二叉树中找到距离为k的两个节点)。 我试图定义两个节点之间的距离,我认为这是沿着树状分支从节点n1移动到节点n2所需的最小节点数。 从这个假设出发,我得到了一个情况,我认为我需要知道每个节点是在根的左边还是右边。 案例1:如果n1和n2位于不同的一侧,那么我爬到根部(距离等于节点n1的深度-假设n1位于左侧),然后向下跑到右侧节点n2(距离等于节点n2的深度)。所以

  • l和r分别是区间的起点和终点。

  • 本文向大家介绍二叉树任意两个节点之间路径的最大长度?相关面试题,主要包含被问及二叉树任意两个节点之间路径的最大长度?时的应答技巧和注意事项,需要的朋友参考一下 考察点:树    

  • 有谁能帮助我理解下面的算法,如何找出二叉树中任意两个节点之间的最大差异。 http://www.geeksforgeeks.org/maximum-difference-between-node-and-its-ancestor-in-binary-tree/ 我不明白为什么他们试图从左子树和右子树得到最小值,而实际上我们想要最大的差异 提前谢谢!!

  • 我想用C++实现这样一个算法,但是任何对解决方案的描述都会很有帮助。

  • 我试图通过在MST中添加新顶点来更新MST。为此,我一直在关注Chin和Houck的“更新生成树”。http://www.computingscience.nl/docs/vakken/al/WerkC/UpdatingSpanningTrees.pdf 论文中的一个步骤要求我在两个给定顶点之间的路径中找到最大的边。我的想法是找到顶点之间所有可能的路径,然后从这些路径中找到最大的边。我一直在尝试在