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.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
/*
二叉树的直径:二叉树中从一个结点到另一个节点最长的路径,叫做二叉树的直径
采用分治和递归的思想:根节点为root的二叉树的直径 = Max(左子树直径,右子树直径,左子树的最大深度(不包括根节点)+右子树的最大深度(不包括根节点)+1)
*/ class Solution {
int res ;
public int diameterOfBinaryTree(TreeNode root) {
res = 0;
getDepth(root);
return res;
} // 此函数是返回树的最大深度
public int getDepth(TreeNode root){
if(root == null){
return 0;
}
int left = getDepth(root.left);
int right = getDepth(root.right);
res = Math.max(res, left+right);
return Math.max(left, right) + 1; }
}

  

LeetCode - Diameter of Binary Tree的更多相关文章

  1. LeetCode——Diameter of Binary Tree

    LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...

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

  4. 【LeetCode】124. Binary Tree Maximum Path Sum 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  5. 【LEETCODE OJ】Binary Tree Postorder Traversal

    Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...

  6. 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  7. 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...

  8. C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解

    剑指offer 面试题39:判断平衡二叉树 提交网址:  http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...

  9. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

随机推荐

  1. Java小程序分析

    public class Helloworld { public static void main(String[] args) { System.out.println("hello wo ...

  2. 软件设计基础-C/S系统

    在软件设计开发过程中,逐渐形成了一些针对特定应用领域的软件系统组织方式的惯用模式 如经典的C/S(client/server,客户/服务器)模式和B/S(browser/server,浏览器/服务器) ...

  3. SharePoint Framework 配置你的SharePoint客户端web部件开发环境

    博客地址:http://blog.csdn.net/FoxDave 你可以使用Visual Studio或者是你自己的开发环境来构建SharePoint客户端web部件.你可以使用Mac.PC或是 ...

  4. switfmailer 邮件时间错误 处理

    Warning: date(): It is not safe to rely on the system's timezone settings. You are *required* to use ...

  5. 10.3制作Android Splash启动界面

    共分三步: 1.制作.9.png图片 可以参考这个文章. 2.修改项目文件,使用.9.png图片 用笔记本打开项目文件,先找到在项目中设置的Splash文件名,并改成第一步制作的.9.png文件名.例 ...

  6. 关于lunece的搜索的分页和多字段搜索关键词

    关于全文检索lunece的分页,我们需要用到的是以下方法 IndexSearch类下的searchAfter方法. IndexSearch isearch=new IndexSearch(a); is ...

  7. Eclipse下SpringBoot没有自动加载application.properties文件

    Eclipse内创建SpringBoot项目,在java/main/resources文件夹下面创建application.properties配置文件,SpringApplication.run后发 ...

  8. 3.Appium处理原生与H5的嵌套

    环境前置准备 手机与电脑USB连接,开启USB调试模式,通过adb devices可查看到此设备. 电脑端.移动端安装chrome浏览器.(尽量保证移动端chrome版本低于电脑端) App webv ...

  9. 【转】如何在win10(64位系统)上安装apache服务器

    如何在win10(64位系统)上安装apache服务器 今天装了Apache服务器,下面是我总结的方法: 一,准备软件 1.64位的apache版本 传送门:http://www.apacheloun ...

  10. Ubuntu16.04安装&创建虚拟环境

    一.linux环境 Ubuntu16.04 二.安装和配置虚拟环境 安装虚拟环境 sudo pip install virtualenv sudo pip install virtualenvwrap ...