LeetCode题解之Diameter of Binary Tree
1、题目描述

2、分析
深度优先。
3、代码
int ans;
int diameterOfBinaryTree(TreeNode* root) {
ans = ;
depth(root); return ans - ;
} int depth(TreeNode *root){
if (root == NULL)
return ;
int L = depth(root->left);
int R = depth(root->right);
ans = max(ans, L+R+);
return max(L,R) + ;
}
LeetCode题解之Diameter of Binary Tree的更多相关文章
- 【LeetCode】543. Diameter of Binary Tree 解题报告 (C++&Java&Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...
- LeetCode题解:(114) Flatten Binary Tree to Linked List
题目说明 Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / \ 2 5 / \ \ 3 ...
- [LeetCode 题解]: Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- [LeetCode 题解]: Minimum Depth of Binary Tree
Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...
- 【leetcode】543. Diameter of Binary Tree
题目如下: 解题思路:最长的周长一定是树中某一个节点(不一定是根节点)的左右子树中的两个叶子节点之间的距离,所以最简单的办法就是把树中所有节点的左右子树中最大的两个叶子节点之间的距离求出来,最终得到最 ...
- LeetCode算法题-Diameter of Binary Tree(Java实现)
这是悦乐书的第257次更新,第270篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第124题(顺位题号是543).给定二叉树,您需要计算树的直径长度. 二叉树的直径是树中 ...
- LeetCode——Diameter of Binary Tree
LeetCode--Diameter of Binary Tree Question Given a binary tree, you need to compute the length of th ...
- [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 124. Binary Tree Maximum Path Sum 、543. Diameter of Binary Tree(直径)
124. Binary Tree Maximum Path Sum https://www.cnblogs.com/grandyang/p/4280120.html 如果你要计算加上当前节点的最大pa ...
随机推荐
- 机器学习技法笔记:12 Neural Network
Roadmap Motivation Neural Network Hypothesis Neural Network Learning Optimization and Regularization ...
- 机器学习技法笔记:13 Deep Learning
Roadmap Deep Neural Network Autoencoder Denoising Autoencoder Principal Component Analysis Summary
- Alienware-15-R3 装Ubuntu 16.04.3 LTS
前言:Alienware-15-R3默认安装的系统是win10.现在卸载win0,装Ubuntu 16.04.3 LTS. 一.下载Ubuntu 16.04.3 LTS镜像文件,下载地址:https: ...
- 线程安全-005-synchronized其他概念
一.Synchornized锁重入 例子程序: package com.lhy.thread01; public class SyncDouble1 { public synchronized voi ...
- ACM学习<一>
c++指针|指针入门 什么是指针? 其实指针就像是其它变量一样,所不同的是一般的变量包含的是实际的真实的数据,而指针是一个指示器,它告诉程序在内存的哪块区域可以找到数据.这是一个非常重要的概念,有很多 ...
- Spring的Java配置方式—@Configuration和@Bean实现Java配置
Java配置是Spring4.x推荐的配置方式,可以完全替代xml配置. 1.@Configuration 和 @BeanSpring的Java配置方式是通过 @Configuration 和 @Be ...
- flask 压缩json
这样返回的json会被压缩
- Controller:EOS区块链核心控制器
Controller是EOS区块链的核心控制器,其功能丰富.责任重大. 关键字:EOS,区块链,controller,chainbase,db,namespace,using,信号槽,fork_dat ...
- linux下将指定目录加入环境变量的方法
每个用户目录下都有一个对应的.bash_profile比如root用户对应/root/.bash_profile,普通用户cqh对应/home/cqh/.bash_profile,以root用户为例v ...
- 动手实现react Modal组件
Modal组件 长话不多说,接下来让我们来动手实现一个react Modal组件. 我们先来看一下实际效果 Modal的布局 首先,让我们先思考下一个Modal组件的布局是怎么样的. 我们先拿一个基本 ...