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二叉树直径的更多相关文章

  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 二叉树的直径 (C++/Java)

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

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

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

  4. 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 ...

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

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2 ...

  6. 543. Diameter of Binary Tree 二叉树的最大直径

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

  7. [leetcode] 543. Diameter of Binary Tree (easy)

    原题 思路: 题目其实就是求左右最长深度的和 class Solution { private: int res = 0; public: int diameterOfBinaryTree(TreeN ...

  8. 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 ...

  9. 【leetcode_easy】543. Diameter of Binary Tree

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

随机推荐

  1. xgCalendar在ASP.NET中的使用

    1.将wdCalendar文件夹考入项目中 2.在页面中添加引用,见3中head标签中定义 3.配置xgCalendar,两段代码放在一起就是完整的页面 body> <div> &l ...

  2. 【Codeforces】CF 467 C George and Job(dp)

    题目 传送门:QWQ 分析 dp基础题. $ dp[i][j] $表示前i个数分成j组的最大和. 转移显然. 吐槽:做cf题全靠洛谷翻译苟活. 代码 #include <bits/stdc++. ...

  3. ExtJS自定义事件

    1.开发ExtJS组件UI的时候,基本上对于一些操作,就是与后台交互之类的多数都是用户进行点击触发一个事件,在事件的处理器handler里面调具体的业务方法,完成业务数据的处理以及业务流程的流转机制, ...

  4. nginx技术分享 (转)

    原文地址:http://blog.csdn.net/nethibernate/article/details/6628267 Nginx的作用: HTTP Server 反向代理,用于将用户的请求转发 ...

  5. fs和http模块

    fs模块写入文件的方式 导入内置模块     const fs=require("fs")   一.异步写入方式     fs.writeFile("写入文件的路径&qu ...

  6. Tornado之模板

    知识点 静态文件配置 static_path StaticFileHandler 模板使用 变量与表达式 控制语句 函数 块 4.1 静态文件 现在有一个预先写好的静态页面文件 (下载静态文件资源), ...

  7. POI 生成带联动下拉框的excel表格

    参考:https://www.cnblogs.com/cjbbk/p/7527276.html 解决POI3.17 与其它版本的不同的坑:https://blog.csdn.net/Weirdo_zh ...

  8. IdUDPServer 收到4次重复的数据

    IdUDPServer1->Send(RemoteIP, LabeledEdit2->Text.ToInt(), InText, IndyTextEncoding_UTF8()); 我发给 ...

  9. c++builder XE6 Remote Debuger 远程调试

    1.远程目标机器 安装D:\Program Files (x86)\Borland\Remote Debugger\20,没有光盘从已安装的xe6电脑上Bin目录下拷贝文件 bccide.dll bo ...

  10. 让旧版本的 Flash IDE 支持更新的 Flash Player/AIR 功能

    转载:https://blog.zengrong.net/post/1568.html 让旧版本的 Flash IDE 支持更新的 Flash Player/AIR 功能 今天在论坛上看到一篇文章:H ...