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

如何求二叉树的直径?

终翰学
2023-03-14
public class Diameter {
    
    // Klasse BinaryTree nicht modifizieren!
    public static class BinaryTree {
        int value;
        BinaryTree left;
        BinaryTree right;

        BinaryTree(int value) {
            this(value, null, null);
        }

        BinaryTree(int value, BinaryTree left, BinaryTree right) {
            this.value = value;
            this.left = left;
            this.right = right;
        }
    }

    public static int diameter(BinaryTree tree) {
        return 0;
    }

    public static void main(String[] args) {
        case1();
        case2();
        case3();
        case4();
        case5();
        case6();
        case7();
    }

    // Testfälle nicht modifizieren!
    public static void case1() {
        BinaryTree root = new BinaryTree(1,
                new BinaryTree(3,
                        new BinaryTree(7,
                                new BinaryTree(8,
                                        new BinaryTree(9),
                                        null),
                                null),
                        new BinaryTree(4,
                                null,
                                new BinaryTree(5,
                                        null,
                                        new BinaryTree(6)))),
                new BinaryTree(2)
        );
        System.out.println("Case 1: Solution should be 6 -- " + diameter(root));
    }
    
    public static void case2() {
        BinaryTree root = new BinaryTree(1);
        System.out.println("Case 2: Solution should be 0 -- " + diameter(root));
    }
    
    public static void case3() {
        BinaryTree root = new BinaryTree(1,
                new BinaryTree(2),
                null);
        System.out.println("Case 3: Solution should be 1 -- " + diameter(root));
    }
    
    public static void case4() {
        BinaryTree root = new BinaryTree(1,
                new BinaryTree(2),
                new BinaryTree(3));
        System.out.println("Case 4: Solution should be 2 -- " + diameter(root));
    }
    
    public static void case5() {
        BinaryTree root = new BinaryTree(1,
                new BinaryTree(2,
                        new BinaryTree(4),
                        null),
                new BinaryTree(3));
        System.out.println("Case 5: Solution should be 3 -- " + diameter(root));
    }

    public static void case6() {
        BinaryTree root = new BinaryTree(1,
                new BinaryTree(2,
                        new BinaryTree(3,
                                new BinaryTree(4,
                                        new BinaryTree(5,
                                                new BinaryTree(6,
                                                        null,
                                                        new BinaryTree(7)),
                                                null),
                                        null),
                                null),
                        null),
                null);
        System.out.println("Case 6: Solution should be 6 -- " + diameter(root));
    }

    public static void case7() {
        System.out.println("Case 7: Solution should be 0 -- " + diameter(null));
    }

}

我必须编程一个二叉树的直径是一个二叉树中最长的路径。路径的定义方式与图的定义方式相同。请注意,路径不一定要经过根。

在diameter类中编写一个diameter函数,该函数获取二叉树并返回树的直径。

每个节点由一个整数值、一个左子级left和一个右子级right组成。每个子节点要么再次为节点,要么为null

我如何在我的程序中实现它?我的想法是:

public int diameter (Node root)
{
    if (root == null) return 0;
    else return Math.max (
        diameter (root.left), 
        Math.max (
            diameter (root.right),
            height (root.left) + height (root.right) + 1));
}

public int height (Node root)
{
    if (root == null) return 0;
    else return 1 + Math.max (height (root.left), height (root.right));
}

共有1个答案

邢俊悟
2023-03-14

我们可以先做深度,然后每一层都增加高度。并从左或右选择最大值。而且我们还会继续跟踪left+right并选择最大的一个。

public static int max = 0;
public static int diameter(BinaryTree root, int height) {
    if (root == null) return height-1;

    int left = diameter(root.left, height + 1);
    int right = diameter(root.right, height + 1);
        
    int diameter = left+right-height*2; 
    max = max < diameter ? diameter : max;
        
    return Math.max(left,right);
}

这样称呼:

max=0;
diameter(root,0);
 类似资料:
  • 我一直在尝试从Node切换到Java,我想知道的一件事是如何以类似于Node显示的格式打印对象,例如二叉树。例如,我的二叉树初始化代码如下: 在节点中,此二叉树将显示如下: 然而在Java,当我做system.out.println(树); 输出->BinaryTree@4554617c 什么是打印我的BinaryTree的正确方法?什么是好方法?有没有一种方法可以用JSON格式打印树?

  • 我尝试按如下方式执行二叉树的垂直顺序遍历:1)找出每个节点与根节点之间的最小和最大水平距离2)创建一个hashmap,将水平距离映射到相应的节点(Map) 然而,我得到了不想要的输出,我认为在实现中有一些错误,因为算法对我来说似乎是正确的。 以下是完整的代码: 输出:{-1=[99999],0=[99999,12],-2=[99999],1=[99999],2=[99999]}那么我的apProc

  • 二叉树 二叉树采用二叉链表存储,要求根据给定的先序遍历序列和中序遍历序列建立二叉树,并输出后序遍历序列、结点总数、叶子数、度为1的结点数、度为2的结点数。 输入格式: 测试数据有多组,处理到文件尾。每组测试数据的第一行输入结点数n(1≤n≤10),第二、三行各输入n个整数,分别表示二叉树的先序遍历序列和中序遍历序列。 输出格式: 对于每组测试,在一行上分别输出该二叉树的后序遍历序列,结点总数,叶子

  • 主要内容:什么是二叉排序树?,使用二叉排序树查找关键字,二叉排序树中插入关键字,二叉排序树中删除关键字,总结前几节介绍的都是有关静态 查找表的相关知识,从本节开始介绍另外一种查找表—— 动态查找表。 动态查找表中做查找操作时,若查找成功可以对其进行删除;如果查找失败,即表中无该关键字,可以将该关键字插入到表中。 动态查找表的表示方式有多种,本节介绍一种使用树结构表示动态查找表的实现方法—— 二叉排序树(又称为 “二叉查找树”)。 什么是二叉排序树? 二叉排序树要么是空 二叉树,要么具有如下特点:

  • 树的特征和定义 树(Tree)是元素的集合。我们先以比较直观的方式介绍树。下面的数据结构是一个树: 树有多个节点(node),用以储存元素。某些节点之间存在一定的关系,用连线表示,连线称为边(edge)。边的上端节点称为父节点,下端称为子节点。树像是一个不断分叉的树根。 每个节点可以有多个子节点(children),而该节点是相应子节点的父节点(parent)。比如说,3,5是6的子节点,6是3,

  • 本文向大家介绍如何知道二叉树的深度?相关面试题,主要包含被问及如何知道二叉树的深度?时的应答技巧和注意事项,需要的朋友参考一下 考察点:二叉树   实现二叉树的深度方式有两种,递归以及非递归。 ①递归实现: 为了求树的深度,可以先求其左子树的深度和右子树的深度,可以用递归实现,递归的出口就是节点为空。返回值为0; ②非递归实现: 利用层次遍历的算法,设置变量level记录当前节点所在的层数,设置变