LeetCode543.二叉树的直径
题目
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.二叉树的直径的更多相关文章
- [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 ...
- Leetcode543.Diameter of Binary Tree二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 : 给定二叉树 1 / \ 2 3 / \ 4 5 返回 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 ...
- [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 ...
- 14.Diameter of Binary Tree(二叉树的直径)
Level: Easy 题目描述: Given a binary tree, you need to compute the length of the diameter of the tree. ...
- Leetcode 543.二叉树的直径
二叉树的直径 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 3, ...
- 543 Diameter of Binary Tree 二叉树的直径
给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点.示例 :给定二叉树 1 / \ 2 ...
- [LeetCode] 543. 二叉树的直径 ☆(递归、数最大深度)
描述 给定一棵二叉树,你需要计算它的直径长度.一棵二叉树的直径长度是任意两个结点路径长度中的最大值.这条路径可能穿过根结点. 示例 :给定二叉树 1 / \ 2 3 / \ 4 5 返回 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 ...
随机推荐
- Day5 - 07 函数的参数-参数组合
现在我们学完了位置参数.默认参数.可变参数.关键字参数.命名关键字参数五种参数类型.在Python中定义函数,可以使用这五种参数进行组合.但是参数定义的顺序必须是:必选参数.默认参数.可变参数.命名关 ...
- Oracle数据泵常用命令
导读:expdp和impdp是oracle数据库之间移动数据的工具,本文简单总结了数据泵的常用命令,希望对大家有帮助. 前言 expdp和impdp是oracle数据库之间移动数据的工具.expd ...
- js下 Day08、DOM案例
一.摇一摇 效果图:
- 移动端 canvas统计图
一.折线图 1. 获取画布画笔 2. 封装画线的方法 3. 画坐标轴 4. 循环数据画横轴 > 4.1 画刻度 > 4.2 刻度文字 > 4.3 画折线 > 4.4 画点 5. ...
- sychronized的实现原理和应用
一.synchronized的使用 1.1修饰方法 public synchronized void method() { // todo } 1.2修饰代码块 public void run() { ...
- Spring-步入Spring旅途
一.Spring前言 讲Spring之前先写段代码,体会一下Java创建对象的方式,这块你理解了对后面有好处! 1.原始时代-new对象 直接new创建对象,代码如下: //User.java pac ...
- 神奇的 SQL 之擦肩而过 → 真的用到索引了吗
开心一刻 今天下班,骑着青桔电动车高高兴兴的哼着曲回家,感觉整个世界都是我的 刚到家门口,还未下车,老妈就气冲冲的走过来对我说道:"你表哥就比你大一岁,人家都买了奔驰了,50 多万!&quo ...
- java中根据后端返回的数据加载table列表
<%//引入 js @ page language="java" pageEncoding="UTF-8"%> <!DOCTYPE HTML& ...
- 将从数据库查询出来的带有父子结构的list转换成treeList结构
package test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedSta ...
- 听说又有兄弟因为用YYYY-MM-dd被锤了...
还记得去年分享过一篇日期格式化使用 YYYY-MM-dd 的潜在问题的文章不? 历史又重演了... 事故现场 我们来写个单元测试,重现一下这个问题. 测试逻辑: 创建两个日期格式化,一个是出问题的YY ...