原题

思路:

题目其实就是求左右最长深度的和

class Solution
{
private:
int res = 0; public:
int diameterOfBinaryTree(TreeNode *root)
{
dfs(root);
return res;
} int dfs(TreeNode *root)
{
if (root == NULL)
{
return 0;
}
int leftNum = dfs(root->left);
int rightNum = dfs(root->right);
res = max(res, leftNum + rightNum);
return max(leftNum, rightNum) + 1;
}
};

[leetcode] 543. Diameter of Binary Tree (easy)的更多相关文章

  1. LeetCode 543. Diameter of Binary Tree (二叉树的直径)

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  2. [leetcode]543. Diameter of Binary Tree二叉树直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  3. [LeetCode] 543. Diameter of Binary Tree 二叉树的直径

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

  4. LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

    题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of ...

  5. [leetcode]543. Diameter of Binary Tree二叉树的直径

    题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ...

  6. leetcode 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)

    124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...

  7. 【leetcode_easy】543. Diameter of Binary Tree

    problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...

  8. 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  9. [LeetCode&Python] Problem 543. Diameter of Binary Tree

    Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a b ...

随机推荐

  1. 常见的几个Qt编程问题的处理(转自QT中文论坛)(挺实用的)

    1.如何在窗体关闭前自行判断是否可关闭答:重新实现这个窗体的closeEvent()函数,加入判断操作 void MainWindow::closeEvent(QCloseEvent*event){i ...

  2. XP下安装ubuntu

    一,环境说明 dell vostro 1400笔记本,winxp sp3操作系统,ubuntu-9.10-desktop-i386.iso 写这篇随笔的时候我用的已经是ubuntu了. 我是在我的移动 ...

  3. 《C++ Primer》读书笔记 第二章

    1.在算数表达式中最好不要使用char或bool,只有在存放字符或布尔值时才使用他们,因为char在有些机器上是有符号的,在一些机器上是无符号的,所以特别容易出问题,如果只表示一个不大的整数,那么明确 ...

  4. 条款14:在资源管理类中小心copying行为

    请牢记: 1.复制RAII对象必须一并复制它所管理的资源,所以资源的copying行为决定RAII对象的copying行为. 2.普遍常见的RAII class copying行为是:抑制copyin ...

  5. 设置windows服务依赖项

    场景还原:python2.7开发的项目,制作成了windows服务,随系统启动.系统重启后发现服务未能自动启动,检查事件查看器日志发现服务先于Mysql数据库服务启动,由于服务中必须对MySQL进行访 ...

  6. asp.net core系列 66 Dapper介绍--Micro-ORM

    一.概述 目前对于.net的数据访问ORM工具很多,EF和EF Core是一个重量级的框架.最近在搭建新的项目架构,来学习一下轻量级的数据访问ORM工具Dapper.Dapper支持SQL Serve ...

  7. 高并发 Nginx+Lua OpenResty系列(3)——模块指令

    Nginx Lua 模块指令 Nginx共11个处理阶段,而相应的处理阶段是可以做插入式处理,即可插拔式架构:另外指令可以在http.server.server if.location.locatio ...

  8. 自定义实现一个loghub(或kafka)的动态分片消费者负载均衡?

    一般地,像kafka之类的消息中间件,作为一个可以保持历史消息的组件,其消费模型一般是主动拉取方式.这是为了给消费者足够的自由,回滚或者前进. 然而,也正是由于将消费消息的权力交给了消费者,所以,消费 ...

  9. vsphere网络

    物理网络 物理机间建立的网络,VMware ESXi运行于物理机之上 虚拟网络 单台物理机上运行的虚拟机之间通信形成的逻辑网络. 一.网络概述 1. 物理以太网交换机 2.vSphere标准交换机 虚 ...

  10. kali 开启键盘背光灯 && 自定义开启终端

    新建文件保存扩展名为.sh 编辑文件,键入如下代码 #!/bin/bash - status=/tmp/keyboard_led_status if [[ ${1} == "on" ...