LeetCode——Diameter of Binary Tree
LeetCode——Diameter of Binary Tree
Question
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.
解题思路
求树的直径意思就是求树的左子树的高度+右子树的高度。 然后遍历所有节点,找到最大值作为直径。
具体实现
class Solution {
public:
// 左子树的高度+右子树的高度
int max_height = 0;
int diameterOfBinaryTree(TreeNode* root) {
if (!root) {
return 0;
}
int left = height(root->left);
int right = height(root->right);
int tmp = left + right;
if (tmp > max_height)
max_height = tmp;
diameterOfBinaryTree(root->left);
diameterOfBinaryTree(root->right);
return max_height;
}
int height(TreeNode* root) {
if (!root) {
return 0;
}
else {
int i = height(root->left);
int j = height(root->right);
if (i >= j) {
return ++i;
} else {
return ++j;
}
}
}
};
LeetCode——Diameter of Binary Tree的更多相关文章
- [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 - 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 ...
随机推荐
- FZU1465
题目链接:传送门 题目大意:给你n个整数(可正可负),求有多少个连续的子序列的和==m(时限1S) 题目思路:前缀和+手写hash(map效率太慢,会超时) 具体做法是用一个数组sum,数组的第i位保 ...
- IntelliJ中的main函数和System.out.println()快捷输入方式
转自:https://blog.csdn.net/assassinsshadow/article/details/73557375 main快捷输入 psvm System.out.println() ...
- mysql数据库导入到oracle数据库
首先,写一个cmd脚本 xx.cmd sqlldr username/password control=xx.ctl errors=10000000 direct=y 再写一个bat脚本xx.bat ...
- .then()
reference: http://www.html-js.com/article/Study-JavaScript-jQuery-Deferred-and-promise-every-day 1.5 ...
- HDU 1863 畅通工程(Kruskal)
畅通工程 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 从一个git仓库迁移到另外一个git仓库
1 从原地址克隆一份裸版本库,比如原本托管于 GitHub. git clone --bare git://github.com/username/project.git git操作的结果会有一个XX ...
- paper reading:gaze tracking
https://www.cv-foundation.org/openaccess/content_cvpr_2016/papers/Krafka_Eye_Tracking_for_CVPR_2016_ ...
- selectedIndex返回被选中的option的index.
/ <label for="city">城市</label> <select id="city" name="schoo ...
- easymake cmake xmake nmake ...
最简单的Makefile,但是还是大程序少不了makefile工具 #CC=arm-linux-gnueabihf-CC=target: $(CC)gcc -o algo_main algo_m ...
- 我们为什么使用ORM
我们为什么使用ORM? http://www.cnblogs.com/tansm/archive/2006/06/07/419927.html 博客园在推广ORM方面的确做了很大的贡献,很多的程序员开 ...