码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • leetcode543--二叉树的直径


    1. 题意

    求二叉树上最远两个节点之间的距离。

    2. 题解

    2.1 暴力

    最长路径的三种情况

    • 通过根节点
    • 在左子树
    • 在右子树
                1
             2    
         4     5  
      6  7   8   9 
      diameter =  5
    
    • 1
    • 2
    • 3
    • 4
    • 5

    通过根节点的最长路径长度一定是左右子树深度之和。

    但是这样求左右子树的深度会不断重复,所以复杂度很高。

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        int getDepth(TreeNode *root) {
            return root == nullptr ? 0 : max(getDepth(root->left), getDepth(root->right)) + 1;
        }
    
        int diameterOfBinaryTree(TreeNode* root) {
    
    // 最长路径的三种情况
    // * 通过根节点
    // * 在左子树
    // * 在右子树
    
    //          1
    //       2    
    //     4  5  
    //  6 7  8 9
    // diameter =  5
            if ( nullptr == root)
                return 0;
            
            int lmx = diameterOfBinaryTree(root->left);
            int rmx = diameterOfBinaryTree(root->right);
    
            int ans = max(lmx, rmx);
            int ldepth = getDepth(root->left);
            int rdepth = getDepth(root->right);
    
    
            return max(ans, ldepth + rdepth); 
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    2.2 动态规划

    我们可以在求深度的时候,更新答案,返回经过根节点但不拐弯的链的最长长度。

    
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
     *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
     *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
     * };
     */
    class Solution {
    public:
        // 自底向上求出,经过当前节点的最大深度
        // 并统计经过当前节点的路径长度
        // 返回经过子树的深度
    
        int getDepth(TreeNode *root, int &ans) {
            if ( nullptr == root)
                return 0;
            int ldepth = getDepth(root->left, ans);
            int rdepth = getDepth(root->right, ans);
    
            ans = max( ldepth + rdepth + 1, ans);
            
            return max(ldepth, rdepth) + 1;
        }
    
        int diameterOfBinaryTree(TreeNode* root) {
            int ans = 0;
    
            getDepth(root, ans);
            
            return ans - 1;
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    Java core——java四种引用详解
    Windows网络管理及诊断命令整理
    用iPad开发iPhone App,苹果发布Swift Playgrounds 4
    Java#11(String中的常用方法)
    暑期JAVA学习(35)线程通信
    VoLTE基础自学系列 | 什么是VoLTE中的透明数据及非透明数据?
    大学生静态HTML鲜花网页设计作品 DIV布局网上鲜花介绍网页模板代码 DW花店网站制作成品 web网页制作与实现
    SpringBoot启动项目报错 Consider defining a bean of type ‘xxx‘ in your configuration
    LeetCode 第113 双周赛补题
    Java审计对比工具JaVers使用
  • 原文地址:https://blog.csdn.net/bdn_nbd/article/details/138166345
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号