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 ...
随机推荐
- DotNet软件开发框架
这是我4月份发在donews博客上的文章,现在都转到博客园来,风满袖希望进一步阐述你的架构,我就将这篇文章转移到博客园.原文:http://blog.donews.com/shanyou/archiv ...
- MVC 多种 数据验证 post
技术:c# .net 采用mvc框架,实现model的数据验证. 刚开始觉得数据验证很方便,可以判断非空.数据正确性,但是后来发现很多需要数据库的判定还是需要post请求做,但是就想mvc的数据验证 ...
- iOS 保存异常日志
// // AppDelegate.m // test // // Created by Chocolate. on 14-4-16. // Copyright (c) 2014年 redasen. ...
- [hihoCoder] 题外话·堆
A direct applicatin of the heap data structure. Specifically, a max heap is used. The required funct ...
- QQ能上,网页打不开
这是一个老问题了,在大学的时候就经常碰到有人问这样的问题,今天写出来祭奠一下,姑凉长点心吧~! 安阳地区DNS:网通202.102.224.68 如果你是电信:222.88.88.88或者直接弄成顶级 ...
- window下搭建python开发环境
搭建一个python开发环境比较简单,所以就稍微记录一下. 1.下载python然后安装 2.配置环境变量 3.在eclipse添加PyDev插件 1.下载python 官网:https://www. ...
- Centos6.5下DHCP服务器的安装和配置
1.首先需要安装DHCP的软件包,使用yum进行安装 # yum install -y dhcp.x86_64 dhcp-devel.x86_64 2.将/usr/share/doc/dhcp-4. ...
- git 学习(一)初始化和提交
git 学习(一) 创建git版本库 $ mkdir gitstudy $ cd gitstudy $ git init nitialized empty Git repository in /Use ...
- js中的整除运算
Math.ceil(count / pagesize); //向上整除 4/3=2; Math.floor(count / pagesize); //向下整除 4/3=1; Math.roun ...
- MySQL给字段唯一索引的三种方法
建表时添加 DROP TABLE IF EXISTS `student`; CREATE TABLE `student` ( `stu_id` ) NOT NULL AUTO_INCREMENT, ` ...