首页 > 其他分享 >二叉树的直径和最大距离问题

二叉树的直径和最大距离问题

时间:2022-10-04 16:34:53浏览次数:69  
标签:head right maxHeight int max 距离 二叉树 直径 left

二叉树的直径和最大距离问题

作者:Grey

原文地址:

博客园:二叉树的直径和最大距离问题

CSDN:二叉树的直径和最大距离问题

二叉树的直径

给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径(边)长度中的最大值。

题目链接:LeetCode 543. Diameter of Binary Tree

主要思路:

定义如下数据结构

public static class Info {
    public int maxHeight; // 从当前节点插到最底部最大高度
    public int max; // 当前树的直径

    public Info(int max, int maxHeight) {
        this.max = max;
        this.maxHeight = maxHeight;
    }
}

其中max为当前树的直径,maxHeight为从当前节点插到最底部的最大高度;

假设以 head 为头的二叉树,左树为 left,右树为 right,

接下来开始整理以 head 为头的二叉树直径的可能性:

可能性1,以 head 为头的二叉树的直径可能只来自做左树,即: left.max。

可能性2,以 head 为头的二叉树的直径可能只来自做左树,即: right.max。

可能性3,以 head 为头的二叉树的直径可能只来自做左树,即: right.maxHeight + left.maxHeight。

所以,以 head 为头的二叉树直径最终为上述三种可能性的最大值,即

Math.max(Math.max(right.max,left.max),right.maxHeight + left.maxHeight);

接下来整理以 head 为头,能插到最底部的最大高度的可能性:

可能性1,head 节点能插入到左树最底部的最大高度是left.maxHeight + 1

可能性2,head 节点能插入到右树最底部的最大高度是right.maxHeight + 1

以上两种可能性求最大值即可,即

Math.max(left.maxHeight, right.maxHeight) + 1

完整代码如下

class Solution {
    public static int diameterOfBinaryTree(TreeNode head) {
        if (head == null) {
            return 0;
        }
        return process(head).max;
    }

    public static class Info {
        public int maxHeight; // 从当前节点插到最底部最大高度
        public int max; // 当前树的直径长度

        public Info(int max, int maxHeight) {
            this.max = max;
            this.maxHeight = maxHeight;
        }
    }

    private static Info process(TreeNode head) {
        if (head == null) {
            return new Info(0, 0);
        }
        Info left = process(head.left);
        Info right = process(head.right);
        int max = Math.max(left.maxHeight + right.maxHeight, Math.max(left.max, right.max));
        int maxHeight = Math.max(left.maxHeight, right.maxHeight) + 1;
        return new Info(max, maxHeight);
    }
}

二叉树节点间的最大距离问题

题目链接: 牛客:二叉树节点间的最大距离问题

主要思路:

这个问题和上述问题类似,只不过在求上述问题的时候,我们定义两个节点的距离是以边为准,而本问题是以两个节点之间的节点数量为准,而两个节点之间

边的数量 + 1 = 节点数量

所以我们可以基于上述的问题的代码,方便得到本题的代码,核心代码如下

    public static Info process(TreeNode head) {
        if (head == null) {
            return new Info(0, 0);
        }
        Info left = process(head.left);
        Info right = process(head.right);
        int max = Math.max(left.maxHeight + right.maxHeight + 1, Math.max(left.max, right.max));
        int maxHeight = Math.max(left.maxHeight, right.maxHeight) + 1;
        return new Info(max, maxHeight);
    }

和上述问题唯一不同的代码就是如下逻辑,上述问题是

int max = Math.max(left.maxHeight + right.maxHeight, Math.max(left.max, right.max));

而本题的逻辑是

int max = Math.max(left.maxHeight + right.maxHeight + 1, Math.max(left.max, right.max));

边的数量 + 1 = 节点数量

完整代码如下:


import java.util.HashMap;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String firstLine = sc.nextLine();
        String[] s = firstLine.split(" ");
        int n = Integer.valueOf(s[0]);
        int rootVal = Integer.valueOf(s[1]);
        HashMap<Integer, TreeNode> map = new HashMap<>();
        TreeNode root = new TreeNode();
        map.put(rootVal, root);

        //构建二叉树
        for (int i = 0; i < n; i++) {
            String line = sc.nextLine();
            String[] str = line.split(" ");
            int faVal = Integer.valueOf(str[0]);
            int lchVal = Integer.valueOf(str[1]);
            int rchVal = Integer.valueOf(str[2]);

            TreeNode curNode = map.get(faVal);

            if (lchVal != 0) {
                TreeNode lch = new TreeNode();
                curNode.left = lch;
                map.put(lchVal, lch);
            }
            if (rchVal != 0) {
                TreeNode rch = new TreeNode();
                curNode.right = rch;
                map.put(rchVal, rch);
            }
        }

        Info info = process(root);
        System.out.println(info.max);


    }

    public static Info process(TreeNode head) {
        if (head == null) {
            return new Info(0, 0);
        }
        Info left = process(head.left);
        Info right = process(head.right);
        int max = Math.max(left.maxHeight + right.maxHeight + 1, Math.max(left.max, right.max));
        int maxHeight = Math.max(left.maxHeight, right.maxHeight) + 1;
        return new Info(max, maxHeight);
    }


    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
    }

    private static class Info {
        int maxHeight;
        int max;

        public Info(int max, int maxHeight) {
            this.max = max;
            this.maxHeight = maxHeight;
        }
    }

}

更多

算法和数据结构笔记

标签:head,right,maxHeight,int,max,距离,二叉树,直径,left
From: https://www.cnblogs.com/greyzeng/p/16753978.html

相关文章

  • 二叉树的直径
    二叉树的直径题目给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过也可能不穿过根结点。代码var......
  • 常见距离算法
    机器学习中有很多的距离计算公式,用于计算数据和数据之间的距离,进而计算相似度或者其他。1.欧式距离(EuclideanDistance)​ 欧式距离是最常见的距离度量方法。小学、初中、......
  • DataStructure_树的基本性质(m叉树和二叉树)
    文章目录​​基本性质​​​​任意树的通用性质​​​​树的定义​​​​边(分支)​​​​结点数和边数的关系​​​​二叉树结点和边的关系​​​​m叉树的结点数量最多为:......
  • C++----二叉树的进阶
    文章目录​​前言​​​​一、二叉搜索树​​​​2.1二叉搜索树概念​​​​2.2二叉树节点​​​​2.3二叉搜索树操作​​​​1.二叉搜索树的查找​​​​2.二叉搜索树......
  • 15分钟精通二叉树,二叉树的先序,中序,后序,层次遍历顺序
    学习目标:......
  • 前序中序构建二叉树 OFF7
    /***Definitionforabinarytreenode.*structTreeNode{*intval;*TreeNode*left;*TreeNode*right;*TreeNode(intx):val(x),......
  • PTA 21级数据结构与算法实验5—树和二叉树
    目录7-1还原二叉树7-2朋友圈7-3修理牧场7-4玩转二叉树7-5根据后序和中序遍历输出先序遍历7-6完全二叉树的层序遍历7-7列出叶结点7-8部落7-9建立与遍历二叉树7-10......
  • 加工大长度、大直径内孔用镗孔工具
    在加工大型圆锥破碎机的锥形铜套(见图1)时遇到了工艺难题:如在W200镗床上加工铜套的锥形内孔,镗杆行程不足(最大行程1600mm),且无法加工锥形孔;如在车床上采用加长刀杆的方法加......
  • #yyds干货盘点# 面试必刷TOP101:编辑距离(一)
    1.简述:描述给定两个字符串str1和str2,请你算出将str1转为str2的最少操作数。你可以对字符串进行3种操作:1.插入一个字符2.删除一个字符3.修改一个字符。字符串长度满......
  • 【ML算法基础】马氏距离
       直观解释(x−μ)(\bold{x}-\bold{\mu})(x−μ)本质上是向量与平均值的距离。然后,将其除以协方差矩阵(或乘以协方差矩阵的逆数)。这实际上是多元变量的常规标......