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 ...
随机推荐
- 【Jmeter 压测MySql连接问题】
JDBC Request :Cannot load JDBC driver class 'com.mysql.jdbc.Driver'解决办法 在Jmeter中run JDBC Request时, ...
- easyui textbox validatebox 默认显示tip提示
easyui验证是不显示tip提示的,验证不通过时只将输入框变红,要触发一个mouse(鼠标放上去)的事件才会显示tip. 现在想要验证不通过时直接显示提示,给用户更加直观的错误提示. 找到源代码中验 ...
- AWT08-绘图
1.组件绘图原理 Java GUI能展示出不同对话框.窗口等等组件外观的本质其实就是绘图. 在AWT中,真正提供绘图功能的是Graphics对象,在Component中提供了三个方法来完成组件图形的绘 ...
- Flink集群监控
prometheus+grafana 监控hadoop.yarn https://blog.csdn.net/c275090933/article/details/82108014 Prometheu ...
- Python高级语法-GIL-理解(4.1.1)
@ 目录 1.结论 2.代码 关于作者 1.结论 当一个py文件执行多进程的时候,是真的在并发 当一个文件在使用多线程的时候,伪并发,在同一时刻只有一个线程执行,因为GIL Guido都说了,不好除这 ...
- robotframework中的参数展开
robot调用关键字传参的方式是用分隔符分开不同参数,如 keyword arg1 arg2 arg3 arg4 当参数中传入了使用@符号的列表变量时,@符号会将列表展开: @{list1}= Cre ...
- 它听键盘声就知道你敲的是什么——GitHub 热点速览 Vol.51
作者:HelloGitHub-小鱼干 本以为本周的 GitHub 和十二月一样平平无奇就那么度过了,结果 BackgroundMattingV2 重新刷新了本人的认知,还能这种骚操作在线实时抠视频去背 ...
- MySQL优化索引
1. MySQL如何使用索引 索引用于快速查找具有特定列值的行.如果没有索引,MySQL必须从第一行开始,然后遍历整个表以找到相关的行.表越大,花费越多.如果表中有相关列的索引,MySQL可以快速确 ...
- 安装cmake过程g++: 错误:unrecognized command line option ‘-std=gnu++14’
问题根因 这个错误一般是gcc/g++版本太低导致的 疑问 我本地明明安装的是高版本的gcc/g++为何说是低版本的呢,有图为证: 这主要是因为你安装了多个版本的gcc/g++,但是默认(/usr/b ...
- [每日一题]面试官问:Async/Await 如何通过同步的方式实现异步?
关注「松宝写代码」,精选好文,每日一题 时间永远是自己的 每分每秒也都是为自己的将来铺垫和增值 作者:saucxs | songEagle 一.前言 2020.12.23 日刚立的 flag,每日一 ...