Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度。一棵二叉树的直径长度是任意两个结点路径长度中的最大值。这条路径可能穿过根结点。
示例 :
给定二叉树
1
/ \
2 3
/ \
4 5
返回 3, 它的长度是路径 [4,2,1,3] 或者 [5,2,1,3]。
注意:两结点之间的路径长度是以它们之间边的数目表示。
class Solution {
public:
    int res = 0;
    int diameterOfBinaryTree(TreeNode* root) {
        if(root == NULL)
            return 0;
        res = max(res, GetDepth(root ->left) + GetDepth(root ->right) + 1);
        diameterOfBinaryTree(root ->left);
        diameterOfBinaryTree(root ->right);
        return res - 1;
    }
    int GetDepth(TreeNode* root)
    {
        if(root == NULL)
            return 0;
        return 1 + max(GetDepth(root ->left), GetDepth(root ->right));
    }
};Leetcode543.Diameter of Binary Tree二叉树的直径的更多相关文章
- [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 ... 
- [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 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 ... 
- 543 Diameter of Binary Tree 二叉树的直径
		给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ... 
- [leetcode]543. Diameter of Binary Tree二叉树的直径
		题目中的直径定义为: 任意两个节点的最远距离 没想出来,看的答案 思路是:diameter = max(左子树diameter,右子树diameter,(左子树深度+右子树深度+1)) 遍历并更新结果 ... 
- [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 ... 
- 543. Diameter of Binary Tree 二叉树的最大直径
		[抄题]: Given a binary tree, you need to compute the length of the diameter of the tree. The diameter ... 
- LeetCode543. Diameter of Binary Tree
		Description Given a binary tree, you need to compute the length of the diameter of the tree. The dia ... 
- 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 ... 
随机推荐
- jmeter-测试https请求
			找了一个HTTPS请求的网站尝试. 我用的是chrome,点这个小锁(如果是IE也可以在网页上右键,属性,高级,证书) 或 看到下图,点击“复制到文件” 或 把导出的证书打成.store 用此命令进行 ... 
- 如何将制定目录加入到PYTHONPATH
			1 问题背景 TensorFlow Object Detection API 是以Slim为基础实现的,需要将Slim的目录加入PYTHONPATH后才能正确运行. 2 机器环境 window10 a ... 
- java笔试之求最大连续bit数
			功能: 求一个byte数字对应的二进制数字中1的最大连续数,例如3的二进制为00000011,最大连续2个1 输入: 一个byte型的数字 输出: 无 返回: 对应的二进制数字中1 ... 
- PHPExcel SUM 返回0
			使用PHPExcel 导出Excel最后的代码是: $objWriter = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007' ... 
- C++编程规范和编译过程详解
			前言:因为c++基础打得不牢,所以准备花点时间再学一下c++的基础知识,主要是看网易云课堂里面的免费课程,把一些知识点做个笔记记下来. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ... 
- 正则获取html标签字符串中图片地址
			html标签字符串: var htmlStr = "<div class='testClass'><img=http://www.chinanews.com/part/ho ... 
- php完整表单实例
			PHP - 在表单中确保输入值 在用户点击提交按钮后,为确保字段值是否输入正确,我们在HTML的input元素中插添加PHP脚本, 各字段名为: name, email, 和 website. 在评论 ... 
- 力扣算法题—460LFU缓存
			[题目描述] 设计并实现最不经常使用(LFU)缓存的数据结构.它应该支持以下操作:get 和 put. get(key) - 如果键存在于缓存中,则获取键的值(总是正数),否则返回 -1. put(k ... 
- java基础之Character类概述
			Character 类 在对象中包装一个基本类型 char 的值 此外,该类提供了几种方法,以确定字符的类别(小写字母,数字,等等),并将字符从大写转换成小写,反之亦然 构造方法 public Cha ... 
- nginx使用手册--nginx.conf文件配置详解
			#运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; ... 
