[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 binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.
Example:
Given a binary tree
1
/ \
2 3
/ \
4 5
Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].
题目
给定一棵二叉树,求任意两个节点的最长路径长度。
思路
长度的定义是边的个数,不是node的个数
跟 [leetcode]124. Binary Tree Maximum Path Sum二叉树最大路径和 思路一致。
代码
class Solution {
public int diameterOfBinaryTree(TreeNode root) {
/* 要么用个global variable放在class下,要么用长度为1的一维数组来存。
这里因为求edge的数量,初始化为一维数组的default值0是可行的。
*/
int[] diameter = new int[1];
dfs(root, diameter);
return diameter[0];
} private int dfs(TreeNode node, int[] diameter) {
if(node == null){return 0;} int lh = dfs(node.left, diameter);
int rh = dfs(node.right, diameter); diameter[0] = Math.max(diameter[0], lh + rh);
return Math.max(lh, rh) + 1;
}
}
[leetcode]543. Diameter of Binary Tree二叉树直径的更多相关文章
- [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. Diameter of Binary Tree二叉树的直径
题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+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 ...
- 543 Diameter of Binary Tree 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- 543. Diameter of Binary Tree 二叉树的最大直径
[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ...
- [leetcode] 543. Diameter of Binary Tree (easy)
原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...
- 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 ...
- 【leetcode_easy】543. Diameter of Binary Tree
problem 543. Diameter of Binary Tree 题意: 转换一种角度来看,是不是其实就是根结点1的左右两个子树的深度之和呢.那么我们只要对每一个结点求出其左右子树深度之和,这 ...
随机推荐
- BCGcontrolBar(一) MFC界面库简介
原帖地址:http://blog.csdn.net/zw514159799/article/details/9148385 英文原文:http://www.bcgsoft.com/bcgcontrol ...
- boost实现日期时间格式化
#include <iostream> #include<thread> #include<chrono> #include<clocale> #inc ...
- BPM与ESB
BPM:业务流程管理 --监控处理流程的轨迹以及处理过程 开源:JBPM 场景: 1.单一系统的协同工作比如审批流程,请假流程 2.多个系统的集成,复用各个子系统,构建新的处理流程(流程的优化与流程 ...
- VisualSVN:允许修改svn提交日志(pre-revpro-change hook)
有时候需要对之前版本提交的错误的日志信息进行修改或者进行补充描述: 1.在windows 7( 64位 )下使用TortoiseSVN客户端,选中代码目录,点击右键,选择<显示日志> 在出 ...
- hibernate-list
Hibernate多表关联查询类: 1. sql查询两个或两个以上的字段,默认情况下,list中封装的是Object[],长度与所查询的字段数一致.这种方式获取的数据只能通过index下标获取. su ...
- python远程调试及celery调试
部分来自 from: https://www.xncoding.com/2016/05/26/python/pycharm-remote.html 你是否经常要在Windows 7或MAC OS X上 ...
- 【OpenPose-Windows】OpenPose+VS2015+Windows+CUDA8+cuDNN5.1 官方配置教程(转载)
[我的电脑配置] 操作系统:Windows 10 CUDA版本:cuda_8.0.61_win10 cuDNN版本:cudnn-8.0-windows10-x64-v5.1 GPU model:Nvi ...
- caffe openpose/Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields配置(转)
Realtime Multi-Person 2D Pose Estimation using Part Affinity Fields 是CVPR2017的一篇论文,作者称是世界上第一个基于深度学习的 ...
- Java8函数之旅(四) --四大函数接口
前言 Java8中函数接口有很多,大概有几十个吧,具体究竟是多少我也数不清,所以一开始看的时候感觉一脸懵逼,不过其实根本没那么复杂,毕竟不应该也没必要把一个东西设计的很复杂. 几个单词 在学习 ...
- linux numastat的理解
numa的统计数据及理解如下, [root@localhost kernel]# numastat node0 node1numa_hit ...