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

Note: The length of path between two nodes is represented by the number of edges between them.

Accepted
111,412
Submissions
241,330
【解析】:
对于每一个点,left 和 right depth 之和,就等于这个点的最大直径。换一句话说,就是这个点,左边能延伸出去最大值 + 右边能延伸出去的最大值,加一起,就等于这个点的左边最远的点到右边最远的点的距离。 就是最大直径。

关键点:用post order去返回每一个点的depth(在之前depth值上+1),null点就返回0(base case);

    需要两个function,因为getDepth function 返回的都是depth,不是diameter,所以需要diameterOfBinaryTree 来单独返回diameter;

    每一个点的最大直径等于左边的depth 加上 右边的depth,取所有点中最大的值。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
int res = 0;
public int diameterOfBinaryTree(TreeNode root) {
getDepth(root);
return res;
}
public int getDepth(TreeNode root) {
if(root == null) return 0;
int l = getDepth(root.left);
int r = getDepth(root.right);
res = Math.max(res,l+r);
return Math.max(l,r) + 1;
}
}

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. 14.Diameter of Binary Tree(二叉树的直径)

    Level:   Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. ...

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

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

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

  5. 【leetcode_easy】543. Diameter of Binary Tree

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

  6. [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 ...

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

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

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

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

随机推荐

  1. 给定一个数字n,不用for循环实现输出数组 [1,2,3,4,...,n]

    一.for循环方式实现输出[1, 2, 3, ..., n] var n = 5; function test(n){ var arr=[]; for( var i = 1; i <= n; i ...

  2. JavaScript中检测数组的几种方式

    检测一个对象是否为数组的方式有: Array.isArray()          // true或false(es5) toString.call([]);       // [object Arr ...

  3. UVA 1262 Password

    https://vjudge.net/problem/UVA-1262 字典序第k小 注意两点: 1. k-- 2.去重 #include<cstring> #include<cst ...

  4. [洛谷P3527] [POI2011]MET-Meteors

    洛谷题目链接:[POI2011]MET-Meteors 题意翻译 Byteotian Interstellar Union有N个成员国.现在它发现了一颗新的星球,这颗星球的轨道被分为M份(第M份和第1 ...

  5. iOS 网络请求--- AFNetworing的使用

    一.GET请求方式: //1.管理器 AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; ...

  6. Flume 入门--几种不同的Sinks

    主要介绍几种常见Flume的Sink--汇聚点 1.Logger Sink 记录INFO级别的日志,一般用于调试.前面介绍Source时候用到的Sink都是这个类型的Sink 必须配置的属性: 属性说 ...

  7. gradle web项目启动报错: java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener

    严重: Error configuring application listener of class org.springframework.web.util.IntrospectorCleanup ...

  8. 【Codeforces629C】Famil Door and Brackets [DP]

    Famil Door and Brackets Time Limit: 20 Sec  Memory Limit: 512 MB Description Input Output Sample Inp ...

  9. Bzoj4870 [SXOI2017]组合数问题

    Time Limit: 10 Sec  Memory Limit: 512 MBSubmit: 155  Solved: 78 Description Input 第一行有四个整数 n, p, k, ...

  10. [HDU5214]Movie解题报告|小水题大智慧

    Movie Cloud and Miceren like watching movies. Today, they want to choose some wonderful scenes from ...