leetCode题解之求二叉树最大深度
1、题目描述
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node。
输入一个二叉树,求出其最大深度。
二叉树的某个节点的深度定义为根节点到该节点的路径长,根的深度为1,依次类推。最大深度就是离根最远的节点的深度。
2、问题分析
对于二叉树而言,使用递归是最容易的一种解法。
3、代码
int maxDepth(TreeNode* root)
{
if(root == NULL)
return ;
else
return max( +maxDepth(root->left) , +maxDepth(root->right) ) ;
}
leetCode题解之求二叉树最大深度的更多相关文章
- leetCode题解之求二叉树每层的平均值
1.题目描述 Given a non-empty binary tree, return the average value of the nodes on each level in the for ...
- 【LeetCode题解】94_二叉树的中序遍历
目录 [LeetCode题解]94_二叉树的中序遍历 描述 方法一:递归 Java 代码 Python代码 方法二:非递归 Java 代码 Python 代码 [LeetCode题解]94_二叉树的中 ...
- 【LeetCode题解】144_二叉树的前序遍历
目录 [LeetCode题解]144_二叉树的前序遍历 描述 方法一:递归 Java 代码 Python 代码 方法二:非递归(使用栈) Java 代码 Python 代码 [LeetCode题解]1 ...
- leetCode题解之反转二叉树
1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...
- Leetcode题解 - 双指针求n数之和
1. 两数之和 """ 双指针,题目需要返回下标,所以记录一个数字对应的下标 """ class Solution: def twoSum( ...
- 【LeetCode题解】二叉树的遍历
我准备开始一个新系列[LeetCode题解],用来记录刷LeetCode题,顺便复习一下数据结构与算法. 1. 二叉树 二叉树(binary tree)是一种极为普遍的数据结构,树的每一个节点最多只有 ...
- 递归的三部解题曲 关联leetcode 104. 二叉树最大深度练习
递归关心的三点 1. 递归的终止条件 2. 一级递归需要做什么 3. 返回给上一级递归的返回值是什么 递归三部曲 1. 找到递归的终止条件:递归什么时候结束 2. 本级递归做什么:在这级递归中应当完成 ...
- [LeetCode] Binary Tree Maximum Path Sum 求二叉树的最大路径和
Given a binary tree, find the maximum path sum. The path may start and end at any node in the tree. ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
随机推荐
- Java学习之路(八):Set
Set集合概述以及特点: set 是一个不包含重复元素的collection set只是一个接口,一般使用它的子类HashSet,LinkedHashSet,TreeSet HashSet 此类是Se ...
- 使用Intent在活动之间穿梭
使用Intent在活动之间穿梭 1.在com.example.activitytest中创建第二个活动SecondActivity: /** * 第二个活动 */ public class Secon ...
- Android之从TCP/IP、HTTP看Socket通信
1.概念 TCP/IP:属于传输层/网络层协议.手机能够使用联网功能是因为手机底层实现了TCP/IP协议,可以使手机终端通过无线网络建立TCP连接.TCP协议可以对上层网络提供接口,使上层网络数据的传 ...
- android 使用lint + studio ,排查客户端无用string,drawable,layout资源
在项目中点击右键(或者菜单中的Analyze),在出现的右键菜单中有“Analyze” --> “run inspaction by Name ...”.在弹出的搜索窗口中输入想执行的检查类型, ...
- IntelliJ IDEA的黑白色背景切换(Ultimate和Community版本皆通用)
不多说,直接上干货! IntelliJ IDEA的黑白色背景切换 File -> Setting -> Editor -> Colors & F ...
- 谈谈hadoop集群启动时进程那回事
不多说,直接上干货! ==================> 1. 启动./start-dfs.sh的顺序是: namenode,datanode,secondarynamenode. 每一个前 ...
- 生成类似于MongoDB产生的ObjectId
package com.jt.boot.utils; import com.google.common.base.Objects; import java.net.NetworkInterface; ...
- java Html 转 PDF
Html 转 PDF 使用 flying-saucer 插件来完成 导入flying-saucer依赖 <dependency> <groupId>org.xhtmlrende ...
- 页面刷新 location.reload()
* 页面不刷新,但是加了location.reload()后,把炒作失败提示语都刷没了.//成功,不提示,刷新看数据变化ajax success:function(res){ if(res.code= ...
- Spring系列之——spring security
1 搭建springboot 2 配置pom依赖(springboot版本为2.1.3) <dependency> <groupId>org.springframework.b ...