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 ...
随机推荐
- mysql 8.0 改变数据目录和日志目录(一)
一.背景 原数据库数据目录:/data/mysql3306/data,日志文件目录:/data/mysql3306/binlog 变更后数据库目录:/mysqldata/3306/data,日志文件目 ...
- 什么是babel
什么是babel babel是一款基于node开发的工具,其功能是对es6的新语法和新特性进行转码.
- 抢先看:笔者亲历的2020年中国.NET开发者大会活动纪实
2020年中国.NET开发者大会活动纪实 1 2020年12月19日的苏州工业园区,天公作美,阳光明媚,气象迷人,正是一个搞事的好日子.在这里,数百名中国.NET开发者们汇聚一堂,怀揣着激情和梦想,一 ...
- NET Core 使用EF Core的Code First迁移和DBFirst
DBFirst (1)Microsoft.EntityFrameworkCore (2)Microsoft.EntityFrameworkCore.Design (3)Microsoft.Entity ...
- asp.net mvc ajax文件上传
前台页面提交文件 <!DOCTYPE html> <html> <head> <meta name="viewport" content= ...
- 【命令】htop命令
一.Htop的使用简介 大家可能对top监控软件比较熟悉,今天我为大家介绍另外一个监控软件Htop,姑且称之为top的增强版,相比top其有着很多自身的优势.如下: 两者相比起来,top比较繁琐 默认 ...
- Sentinel滑动窗口算法
在前面搞清楚了Sentinel的使用后,大致理了一下Sentinel的责任链,搞清楚了这个,基本就已经梳理清楚sentinel-core模块的大部分内容,顺着这条链路可以继续梳理很多东西. 知其然.知 ...
- idea中surround with
idea中的surround with是把选中的代码块装进一些带有{}的语句中,比如if,try,for等等 快捷键是ctrl+alt+t,先选中代码,再按快捷键,如图
- python-OpenCV 使用GrabCut来实现图片的前景与后景的分割
先上一个效果图: 使用Python3.7+OpenCV 3.x. 需要引入 numpy库. 在图上用鼠标左键和右键标记前景和后景即可.如果需要重新标记图像,关闭程序重新运行. 以下是具体实现代码. # ...
- MongoDb二
获取集合的时候,可以传入bean的class文件.来实现数据的绑定.在这之前.需要自定义一个Codec类.进行数据的转换. MongoClient client=new MongoClient(new ...