题目

 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. IOS中将字典转成模型对象

    作为IOS开发初级者今天学习了 如何将plist数据字典转成 数据对象数组中 .有点像C#中解析xml数据 的过程. apps.plist的xml数据是这样的 <?xml version=&qu ...

  2. git单人本地操作

    git配置个人信息 git config --global user.name "用户名" git config --global user.email "邮箱" ...

  3. js下 Day04、DOM操作--自定义属性

    语法: 元素.getAttribute('自定义属性名') 功能:获取自定义属性 语法: 元素.setAttribute('自定义属性名','值') 功能:设置自定义属性 语法: 元素.removeA ...

  4. [日常摸鱼]bzoj2875[NOI2012]随机数生成器-矩阵快速幂

    好裸的矩阵快速幂-然而我一开始居然构造不出矩阵- 平常两个的情况都是拿相邻两项放在矩阵里拿去递推的-然后我就一直构造不出来-其实把矩阵下面弄成1就好了啊orz #include<cstdio&g ...

  5. Linux系统搭建RabbitMQ

    下载erlang和rabbitmq-server 1.下载Erlang安装包 [root@VM_0_9_centos soft]# wget http://erlang.org/download/ot ...

  6. python 做回归

    1 一元线性回归 线性回归是一种简单的模型,但受到广泛应用,比如预测商品价格,成本评估等,都可以用一元线性模型.y = f(x) 叫做一元函数,回归意思就是根据已知数据复原某些值,线性回归(regre ...

  7. 【k8s实战一】Jenkins 部署应用到 Kubernetes

    [k8s实战一]Jenkins 部署应用到 Kubernetes 01 本文主旨 目标是演示整个Jenkins从源码构建镜像到部署镜像到Kubernetes集群过程. 为了简化流程与容易重现文中效果, ...

  8. 如何把 Next.js 项目部署到服务器?

    Next.js 是什么? Next.js 是一个用于 生产环境的 React 框架.Next.js 为您提供生产环境所需的所有功能以及最佳的开发体验:包括静态及服务器端融合渲染. 支持 TypeScr ...

  9. [leetcode]24. Swap Nodes in Pairs交换链表的节点

    感觉这个题后台的运行程序有问题,一开始自己想的是反转链表那道题的方法,只是隔一个节点执行一次,但是没有通过,TLE了,但是很奇怪,并没有死循环,就是最后返回的时候超时. 最后的思路就是很简单的进行交换 ...

  10. JAVA程序通过JNI调用C/C++库

    java程序如何调用c/c++库中的函数主要分为三个步骤: 1. 加载库文件. System.loadLibrary 2. 找到函数( java函数<----映射---->c函数 ) 3. ...