二叉树的直径

给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。

示例 :
给定二叉树

1

/ \

2 3

/ \

4 5

返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。

注意:两结点之间的路径长度是以它们之间边的数目表示。

思路:先用的方法是只计算根节点的左右节点的高度,然后返回两个数相加的和,但是发现有些情况并没有通过,是因为可能最长路径并不是通过根节点的,例如左孩子只有一个节点,但是右孩子的左右节点都很多,所以最后的方法是在计算二叉树的高度的时候比较左右子节点的高度的和与当前最长路径比较,最后返回最长路径。

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
int maxNum = 0; public int diameterOfBinaryTree(TreeNode root) {
int n = 0;
if (root == null || (root.left == null && root.right == null)) {
return 0;
}
helper(root);
return maxNum;
} public int helper(TreeNode root) {
if (root == null) {
return 0;
}
int left = helper(root.left);
int right = helper(root.right);
if (left + right > maxNum) {
maxNum = left + right;
}
return left > right ? left + 1 : right + 1; }
}

Leetcode 543.二叉树的直径的更多相关文章

  1. Java实现 LeetCode 543. 二叉树的直径(遍历树)

    543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过也可能不穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / ...

  2. Java实现 LeetCode 543 二叉树的直径

    543. 二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 ...

  3. [LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)

    描述 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, 它的长 ...

  4. LeetCode 543. 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 ...

  5. [LeetCode] 543. 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 ...

  6. Leetcode题目543:二叉树的直径(简单)

    题目描述: 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...

  7. LeetCode 543. Diameter of Binary Tree 二叉树的直径 (C++/Java)

    题目: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of ...

  8. [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 ...

  9. 543 Diameter of Binary Tree 二叉树的直径

    给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树          1         / \        2 ...

随机推荐

  1. Samuraiwtf-的确是很好的渗透学习平台

    有人问我要渗透测试平台学习,我想到了Samurai ,记得里面带有很多的,这里来推广一下. Samurai Web 测试框架很多人说是live CD测试环境,但是现在似乎不是了,至少目前最新版的只有这 ...

  2. 自动释放池的前世今生 ---- 深入解析 autoreleasepool

    http://draveness.me/autoreleasepool.html 关注仓库,及时获得更新:iOS-Source-Code-Analyze Follow: Draveness · Git ...

  3. FETCH - 用游标从查询中抓取行

    SYNOPSIS FETCH [ direction { FROM | IN } ] cursorname where direction can be empty or one of: NEXT P ...

  4. Shell重启Tomcat脚本

    #!/bin/bash echo -e "\n\n\n" #force kill flag,if equal [f] to force kill all flag="He ...

  5. CentOS系统下安装Redis

    1. 安装C语言环境 yum install gcc-c++ 2.下载Redis安装包 http://download.redis.io/releases/redis-3.2.9.tar.gz 3.解 ...

  6. matlplotlib 为折线图填充渐变颜色

    概要   本篇记录绘图时填充颜色时的一些常用设置,主要用到了 imshow,fill 函数.   填充图实例   填充的效果图如下: 图 1:渐变色效果图 可根据下方给出的代码进行自定义. #!/us ...

  7. winform下读取excel文件并绑定datagridview例子

    首先我要读取这个excel文件然后生成Datable 用winform编程的方式 前台界面: 后台的代码 using System; using System.Collections.Generic; ...

  8. css中让元素隐藏的多种方法

    { display: none; /* 不占据空间,无法点击 / } { visibility: hidden; / 占据空间,无法点击 / } { position: absolute; top: ...

  9. mysql 索引的统计

    查看一个库里面没有使用过的索引select object_type,object_schema,object_name,index_name,count_star,count_read,COUNT_F ...

  10. 【前端_js】ajax的应用

    1.设置请求头部 function makeRequest() { alert("inside makeRequest()"); var settings = { type: &q ...