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 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的更多相关文章
- LeetCode——Diameter of Binary Tree
LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...
- [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 ...
- 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】124. Binary Tree Maximum Path Sum 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【一天一道LeetCode】#107. Binary Tree Level Order Traversal II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- 【一天一道LeetCode】#103. Binary Tree Zigzag Level Order Traversal
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: htt ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- (二叉树 递归) 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 ...
随机推荐
- body中的onload()函数和jQuery中的document.ready()有什么区别?
1.我们可以在页面中使用多个document.ready(),但只能使用一次onload(). 2.document.ready()函数在页面DOM元素加载完以后就会被调用,而onload()函数则要 ...
- Animation和Animator 的区别
此文章转载于极视学堂!!!! ①Animation和Animator 虽然都是控制动画的播放,但是它们的用法和相关语法都是大有不同的. Animation 控制一个动画的播放,而Animator是多个 ...
- 【收集资料】OpenGL学习
1.课本配套网站,有Sample Code和书中的图片等 http://math.ucsd.edu/~sbuss/MathCG/ 2.Visual C++的帮助系统可查阅基本OpenGL函数(不包 ...
- Toy Factory
Factory is a design pattern in common usage. Please implement a ToyFactory which can generate proper ...
- 2017 秦皇岛CCPC Balloon Robot (ZOJ 3981)
题意:给出n个队伍,m个座位,q次A题的队伍与时间,下一行是n个队伍的坐的位置,再下面q行就是第x个队再第y秒A出一道题,然后有一个机器人,开始位置由你选,他每走一步 他就会向右走一格,走到m的时候会 ...
- L258 技术转让
We will inform you of the weight, measurements, number of cases, cost of the drawings and other docu ...
- ESP8266上报数据到中国移动物联网平台HTTP
#include <HttpPacket.h> #include <ArduinoJson.h> #include <ESP8266WiFi.h> HttpPack ...
- php优秀框架codeigniter学习系列——CI_Utf8类
CI_Utf8类用来对Utf8编码环境提供支持.(Provides support for UTF-8 environments). 从构造函数看,只有当开启了PCRE模式,加载了iconv或者mbs ...
- filebeat成精之路
https://www.cnblogs.com/jingmoxukong/p/8185321.html
- 【Python】爬虫-2
8. urllib2.urlopen可以接受一个Request对象或者url,(在接受Request对象时候,并以此可以来设置一个URL的headers),urllib.urlopen只接收一个url ...