题目

 1 class Solution {
2 public:
3 int minimum = INT_MIN;
4 vector<int>res;
5 int diameterOfBinaryTree(TreeNode* root) {
6 if(root == NULL) return 0;
7 if(root->left == NULL && root->right == NULL) return 0;
8 if(root->left == NULL) return height(root->right) ;
9 if(root->right == NULL) return height(root->left) ;
10 res.push_back(height(root->left) + height(root->right));
11 diameterOfBinaryTree(root->left);
12 diameterOfBinaryTree(root->right);
13 for(int i = 0;i < res.size();i++){
14 if(res[i] > minimum ) minimum = res[i];
15 }
16 return minimum;
17 //return height(root->left) + height(root->right) ;
18 }
19 int height(TreeNode* root){
20 if(root == NULL) return 0;
21 return max(height(root->left),height(root->right)) + 1;
22 }
23 };

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

  1. [Swift]LeetCode543. 二叉树的直径 | 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 ...

  2. Leetcode543.Diameter of Binary Tree二叉树的直径

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

  3. 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 ...

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

  5. 14.Diameter of Binary Tree(二叉树的直径)

    Level:   Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. ...

  6. Leetcode 543.二叉树的直径

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

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

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

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

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

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

随机推荐

  1. Django DRF 分页

    Django DRF 分页 分页在DRF当中可以一共有三种,可以通过setttings设置,也可也通过自定义设置 PageNumberPagination 使用URL http://127.0.0.1 ...

  2. Spring MVC例子

    学习Spring MVC的一个例子,参考书籍<Servlet.JSP.Spring MVC学习指南>,简单总结一下. 代码下载:https://github.com/PeiranZhang ...

  3. 统计文件行数,推荐使用LineNumberReader

    一.主题: 读取文本文件最大行数性能比较:lineNumberReader > Files.lines 二.code 1 @Test 2 public void testLineReader() ...

  4. DRF类视图让你的代码DRY起来

    刚开始写views.py模块的代码,一般都是用def定义的函数视图,不过DRF更推荐使用class定义的类视图,这能让我们的代码更符合DRY(Don't Repeat Yourself)设计原则: 使 ...

  5. 同学你会hello world吗? 给我讲清楚点

    少点代码,多点头发 本文已经收录至我的GitHub,欢迎大家踊跃star 和 issues. https://github.com/midou-tech/articles 面试官超级喜欢问hello ...

  6. 用matlab提取jpg曲线数据或者jpg图片重新复原

    I = imread('111.jpg');%读取处理好的图片,必须是严格坐标轴线为边界的图片 I=rgb2gray(I); %灰度变化 I(I>200)=255; %二值化 I(I<=2 ...

  7. HCIP --- BGP 总结

    AS:自治系统  --逻辑管理域(例如移动.电信.联通),AS号范围:0-65535,其中,1-64511:公有AS,64512-65535:私有AS IGP:内部网关协议,在一个AS之内传递的路由协 ...

  8. python的常量与变量

    1.Python属于强类型编程语言,Python解释器会根据赋值或运算来自动判断变量的类型.Python还是一种动态类型语言,变量的类型也是可以随时变化的. >>> x=3 > ...

  9. 解决UE4缓存使C盘膨胀的问题

    使用UE4的时候会发现C盘越来越小了,那是因为UE4引擎的缓存文件默认保存在C盘的缘故. 概述 一.出现的问题:UE4的缓存文件会导致C盘膨胀. 二.解决的方式:请严格按照下列步骤来执行.1. 更改U ...

  10. 高效扩展工具让 VS Code 如虎添翼

    Codelf 变量命名神器 Star:10688 https://github.com/unbug/codelf 新建项目,变量,类,方法,接口都需要命名,一个好的命名可以一眼看出这个地方的功能,Co ...