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 ...
随机推荐
- 58、常规控件(1)Floating Action Button-浮动的圆形按钮
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...
- c++ wchar_t
·C语言相关 对应于char, C语言中也有宽字符内型wchar_t.wchar_t被定义为: typedef unsigned short wchar_t ;显然它是16位的.wchar_t类型的常 ...
- 12.php中无比坑爹的构造函数。
当你在php类中,写一个构造方法时,记得,一定要用__这是两个下划线,而不是一个.......... <?php class Car { // function _construct() { / ...
- LIMIT Query Optimization
LIMIT limitFrom , perPageNum LIMIT offset, size MySQL :: MySQL 8.0 Reference Manual :: 13.2.10 SELEC ...
- python系列十七:Python3 标准库概览
#!/usr/bin/python #-*-coding:gbk-*- #Python3 标准库概览'''操作系统接口os模块提供了不少与操作系统相关联的函数.建议使用 "import os ...
- Java基础 - 字符串 String
字符串就是用字符拼接成的文本值,字符串在存储上类似数组,在java语言中把字符串当做对象进行处理 创建字符串 package com.mingri.chapter_02; public class d ...
- 2015-03-22——js常用其它方法
Function Function.prototype.method = function (name, func) { this.prototype[name] = func; //此时th ...
- Java实现对List去重
方式一,使用for循环遍历去除List中的重复元素代码如下 public static void main(String[] args) { Test07 test07 = new Test07(); ...
- 最全的Eclipse使用快捷键
Eclipse 是一种基于 Java 的可扩展开源开发平台.尽管 Eclipse 是使用 Java 语言开发的,但它的用途并不限于 Java 语言,Eclipse 还包括插件开发环境等,下面将为大家介 ...
- 信息安全意识教育日历——By 安全牛
安全牛:企业即使投入再好的信息安全技术和产品,也难以解决内部威胁以及社会工程等攻击手段,无法做到全面有效地保护企业信息资产.而通过开展员工的信息安全意识培训教育工作,不仅能降低企业风险.满足合规要求, ...