Level:

  Easy

题目描述:

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.

思路分析:

  题目要求找出二叉树的直径,二叉树的直径的定义是,任意两节点之间的最长路径,这个路径不一定会经过根节点。我们的解题思路是,最长路径肯定是以某个节点为中转,分别从这个节点的左右子节点延伸到叶子节点的两条无转向的路径和。所以只需要求出所有节点作为中转节点的路径长度,取其中最大的就得到了答案。

代码:

public class TreeNode{
int val;
TreeNode left;
TreeNode right;
public TreeNode(int x){
val=x;
}
}
public class Solution{
public int diameterOfBinaryTree(TreeNode root){
if(root==null)
return 0;
int res=depth(root.left)+depth(root.right);
int ledia=diameterOfBinaryTree(root.left);
int ridia=diameterOfBinaryTree(root.right);
return Math.max(Math.max(res,ledia),ridia);
}
public int depth(TreeNode root){
if(root==null)
return 0;
int left=depth(root.left);
int right=depth(root.right);
return Math.max(left,right)+1;
}
}

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

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

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

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

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

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

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

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

  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. 543. Diameter of Binary Tree 二叉树的最大直径

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

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

随机推荐

  1. [摘]Android逆向分析常用网站

    androidterm:   Android Terminal Emulator   http://code.google.com/p/androidterm/   droidbox:   Andro ...

  2. windows下基于bat的每1分钟执行一次一个程序

    @echo off cls mode con cols=35 lines=6 & color 5B :p call python C:\省局监控\ahwater_perf_monitor.py ...

  3. Solaris11.1网络配置(Fixed Network)

    Solaris11的网络配置与Solaris10有很大不同,Solaris11通过network configuration profiles(NCP)来管理网络配置. Solaris11网络配置分为 ...

  4. VS2015 MSVC编译FFMPEG

    1.下载安装msys2 http://www.msys2.org/下载msys2 下载安装完成后,在msys2的shell中安装编译FFMPEG必要的命令行工具 pacman -S make gcc ...

  5. Solr查询错误

    报错: Exception in thread "main" java.lang.VerifyError: Bad return type Exception Details: L ...

  6. 用C++的基本算法实现十个数排序

    冒泡排序法 原理: 它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来.走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成. 冒泡排序算法的运作如下 ...

  7. SQL IN, NOT IN, EXISTS, NOT EXISTS

    IN与EXISTS执行流程 IN:在查询的时候,首先查询子查询的表,然后将内表和外表做一个笛卡尔积,然后按照条件进行筛选.所以相对内表比较小的时候,in的速度较快.(IN时不对NULL进行处理) EX ...

  8. Angular问题03 @angular/material版本问题

    1 问题描述 应用使用 angular4在使用@angular/material时,若果在导入模块时使用mat开头,就会报错. 2 问题原因 @angular/material版本出现问题,@angu ...

  9. Angular18 RXJS

    1 RX 全称是 Reactive Extensions,它是微软开发并维护的基于 Reactive Programming 范式实现的一套工具库集合:RX结合了观察者模式.迭代器模式.函数式编程来管 ...

  10. Linux如何修改网络环境参数

    如下设置: 检验是否可以连通,就使用ping命令ping 网关开始的时候总是现实unreachable 设置IP:sudo ifconfig eth0 133.133.133.190 netmask ...