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 ...
 
随机推荐
- 【BZOJ1189】[HNOI2007]紧急疏散evacuate 动态加边网络流
			
[BZOJ1189][HNOI2007]紧急疏散evacuate Description 发生了火警,所有人员需要紧急疏散!假设每个房间是一个N M的矩形区域.每个格子如果是'.',那么表示这是一块空 ...
 - 160718、jsoup-1.8.1.jar操作html
			
导入jsoup-1.8.1.jarimport java.io.IOException;import org.jsoup.Connection;import org.jsoup.Jsoup;impor ...
 - Debug技巧:条件断点和远程Debug
			
一.断点: 1.条件断点 本以为条件断点很难,其实就这样. 二.远程Debug 1.首先看服务器是不是在Debug模式下工作 不是则进行(tomcat/bin/catalina.sh)配置. -Xde ...
 - Centos6.5下Samba服务器的安装和配置
			
1.安装samba服务 # yum install samba samba-client samba-swat 2.安装包说明 samba-3.6.23-43.el6_9.x86_64----> ...
 - mysqldump迁移说明
			
使用mysqldump导出数据, 数据包含单行insert,带字段值 #使用mysqldump备份数据到文件, 主要在每个分片的主上面进行备份,确保数据是最新的. mysqldump -h192. - ...
 - MyBatis generator 生成生成dao model mappper
			
MyBatis GeneratorXML配置文件参考 在最常见的用例中,MyBatis Generator(MBG)由XML配置文件驱动. 配置文件告诉MBG: 如何连接到数据库 什么对象要生成,以及 ...
 - 用jq实现鼠标移入按钮背景渐变其他的背景效果
			
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
 - 洛谷 P4145 上帝造题的七分钟2 / 花神游历各国
			
洛谷 这题就是区间开根号,区间求和.我们可以分块做. 我们记布尔数组vis[i]表示第i块中元素是否全部为1. 因为显然当一个块中元素全部为1时,并不需要对它进行根号操作. 我们每个块暴力开根号,因为 ...
 - pandas 如何判断指定列是否(全部)为NaN(空值)
			
判断某列是否有NaN df['$open'].isnull().any() # 判断open这一列列是否有 NaN 判断某列是否全部为NaN df['$open'].isnull().all() # ...
 - Linux下套接字具体解释(九)---poll模式下的IO多路复用server
			
參照 poll调用深入解析-从poll的实现来讲poll多路复用模型,非常有深度 poll多路复用 poll的机制与select相似,与select在本质上没有多大差别.管理多个描写叙述符也是进行轮询 ...