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 ...
随机推荐
- java串口编程
报错:no rxtxSerial in java.library.path thrown while loading gnu.io.RXTXCommDrive java.lang.Unsatisfie ...
- OO第四次课程总结分析
OO第四次课程总结分析 测试与正确性论证的效果差异及优缺点 测试,即使用测试样例来验证我们的程序是否能完成相应功能的过程.测试数据的产生基于前置条件和后置条件,通过执行测试数据检查方法输出是否满足需求 ...
- 当你有双网络(内部网+互联网)时,如何透明NAT给其他电脑上网。虚拟机+爱快
一:简介 具体环境是这样的:单位没有提供互联网连接,都是内部网,linux服务器,无法连接源更新,docker无法pull镜像,python无法在线pip安装包. 真是郁闷到想死啊. 好在我的笔记本有 ...
- Centos7单主机部署 LAMP + phpmyadmin 服务
LAMP -> centos + apache + mysql + php + phpmyadmin 一:搭建yum仓库: 安装utils: yum -y install yum-utils c ...
- 软件工程项目程序:WC
1:代码来源:http://yuncode.net/code/c_5087c8e4cd77190 2:Platform:Eclipse Language:Java 3:Bug:暂时没有 4. Func ...
- normalization 阅读笔记
https://zhuanlan.zhihu.com/p/33173246 阅读笔记 1. normalization whiting - PCA 2. Internal Covariate Shif ...
- ndoe.js 和npm私有仓库的搭建
下载nodejs的压缩包 网址:https://nodejs.org/en/ 下载以tar.xz结尾的包例如:node-v8.9.4-linux-x64.tar.xz 上传包到制定的目录 可以用lrz ...
- 基于CART的回归和分类任务
CART 是 classification and regression tree 的缩写,即分类与回归树. 博主之前学习的时候有用过决策树来做预测的小例子:机器学习之决策树预测--泰坦尼克号乘客数据 ...
- linux command1
#列出指定用户(当前用户)的组信息 groups #将指定的用户添加(-a)到指定的组内(改组必须已经存在)或指定用户从指定的组中删除(-d) gpasswd –a/-d username grou ...
- mac os x lipo 工具
lipo是管理Fat文件的工具,可以查看平台列表,提取.重新打包 dreamdeMac-mini:test dream$ lipo -info libtest001.a Architectures i ...