[LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)
描述
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
示例 :
给定二叉树
1
/ \
2 3
/ \
4 5
返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
注意:两结点之间的路径长度是以它们之间边的数目表示。
解析
直觉是求给定树的左右子树的最大深度和。其实不尽然,也有可能单单在给定树的一边,所以需要记录一下当前子树的左右子树的最大深度和。
代码
private int maxHeight = 0;
public int diameterOfBinaryTree(TreeNode root) {
if (null == root) {
return 0;
}
diameterOfBinaryTreeH(root);
return maxHeight;
} public int diameterOfBinaryTreeH(TreeNode root) {
if (null == root) {
return 0;
}
int leftNum = diameterOfBinaryTreeH(root.left);
int rightNum = diameterOfBinaryTreeH(root.right);
maxHeight = Math.max(leftNum + rightNum, maxHeight);//因为最长的直径也能在当前子树的左右子树上
return Math.max(leftNum, rightNum) + 1;
}
[LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)的更多相关文章
- Java实现 LeetCode 543. 二叉树的直径(遍历树)
543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过也可能不穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / ...
- Java实现 LeetCode 543 二叉树的直径
543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 ...
- Leetcode 543.二叉树的直径
二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...
- LeetCode:二叉树的非递归中序遍历
第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ /* inorder traversal binary tree */ #include <stdi ...
- [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 ...
- 递归的三部解题曲 关联leetcode 104. 二叉树最大深度练习
递归关心的三点 1. 递归的终止条件 2. 一级递归需要做什么 3. 返回给上一级递归的返回值是什么 递归三部曲 1. 找到递归的终止条件:递归什么时候结束 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 ...
- 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 ...
- Leetcode题目543:二叉树的直径(简单)
题目描述: 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...
随机推荐
- SpringMVC异步处理 可使用的返回值类型
CallableMethodReturnValueHandler Callable.class.isAssignableFrom(returnType.getParameterType()); Def ...
- SpringBoot项目从Git拉取代码并完成编译打包启动的sh自动脚本
操作步骤: 1.进入/home/servers/codes/xxxx-dev/目录,从git上将项目clone下来: 2.确保/usr/local/xxx/xxxx-dev目录存在: 3.确保sh脚本 ...
- VS2017 winform 打包 安装(使用 Microsoft Visual Studio 2017 Installer Project)
Microsoft Visual Studio 2017 Installer Projects SkyRiN发表于Coding+订阅 253 助力数字生态,云产品优惠大促 腾讯云促销,1核1G 99元 ...
- PHP MQTT 实践
MQTT介绍:http://mqtt.org 服务器端https://mosquitto.org/download/ PHP客户端https://github.com/bluerhinos/phpMQ ...
- 一次升级jar包遇到的空指针异常
今天自己在升级公司的一个jar后,一直报空指针异常.代码如下 package com.zhuanche.http; import com.alibaba.fastjson.JSON; import c ...
- rtmp_specification_1.0
Copyright Adobe Systems Incorporated H. Parmar, Ed. M. Thornburgh, Ed. Adobe December 21, 2012 Adobe ...
- elasticsearch-head
elasticsearch-head 是用于监控 Elasticsearch 状态的客户端插件,包括数据可视化.执行增删改查操作等 安装前先安装nodejs 1.下载 地址 2.安装 npm ins ...
- [ARM-Linux开发] 嵌入式 linux如何生成ko文件
hello.c文件如下 驱动程序: #include <Linux/***.h> 是在linux-2.6.29/include/linux下面寻找源文件. #include <asm ...
- 在ensp中的acl控制
原理 实验模拟 实验拓扑 相关参数 我们在每一台路由器上设置ospf服务,使其互相能通 下面我们配置基本ACL控制访问 配置完成后,尝试在R1上建立telent连接 但是这样设置是不安全的,只要是直连 ...
- elasticsearch 常见查询及聚合的JAVA API
ES 常见查询 (1)根据ID 进行单个查询 GetResponse response = client.prepareGet("accounts", "person&q ...