LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)
104. 二叉树的最大深度
104. Maximum Depth of Binary Tree
题目描述
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
LeetCode104. Maximum Depth of Binary Tree
示例:
```
3
/ \
9 20
/ \
15 7
```
返回它的最大深度 3。
Java 实现
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class Solution {
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
}
}
相似题目
参考资料
- https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/
- https://leetcode.com/problems/maximum-depth-of-binary-tree/
LeetCode 104. 二叉树的最大深度(Maximum Depth of Binary Tree)的更多相关文章
- [Swift]LeetCode104. 二叉树的最大深度 | 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】【Easy】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刷题笔记】Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- 【easy】104. Maximum Depth of Binary Tree 求二叉树的最大深度
求二叉树的最大深度 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; ...
- leetcode 104 Maximum Depth of Binary Tree二叉树求深度
Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- [LintCode] 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 104. Maximum Depth of Binary Tree C++ 解题报告
104. Maximum Depth of Binary Tree -- Easy 方法 使用递归 /** * Definition for a binary tree node. * struct ...
- 【LeetCode】104. Maximum Depth of Binary Tree (2 solutions)
Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the ...
随机推荐
- 2、Apache(httpd)之一 三种工作模式
httpd的特性: 高度模块化:core + modules 模块化设计DSO:Dynamic Shared Object MPM:Multipath Processing Modules 多路处理模 ...
- elasticsearch x-pack license过期
1.注册一个新的license,每一项都要填写,每次可以使用一年,一年到期后再来注册一个新的 2.更新license (官方文档:https://www.elastic.co/guide/en/x-p ...
- python实用技巧之任务切分
Python 大任务切分小任务 今天来说说,Python中的任务切分.以爬虫为例,从一个存 url 的 txt 文件中,读取其内容,我们会获取一个 url 列表.我们把这一个 url 列表称为大任务. ...
- 分布式缓存Redis集群搭建
redis安装 1.下载tar包至/opt/redis 2.解压tar包 tar -xvf redis-4.0.14.tar.gz 3. cd redis-4.0.14 make一下. 单节点的red ...
- Git的使用(2) —— 本地版本库的操作
1. 向本地版本库中添加文件 注意:.git文件夹是本地版本库,包含.git文件夹的目录叫工作目录,要往本地版本库中添加文件,就必须将文件放在工作目录中. (1) 把文件添加到工作目录中. (2) 右 ...
- 【E2E】Tesseract5+VS2017+win10源码编译攻略
一,记录我目前在win10 X64和VS2017的环境下成功编译Tesseract5.0的方式: 二,记录在VS2017 C++工程中调用Tesseract4.0的方法: 三,记录编译和调用Tesse ...
- Vue导出ZIP
Export2Zip /* eslint-disable */ require('script-loader!file-saver'); import JSZip from 'jszip' expor ...
- Microsoft OA
Given a string S consisting of N lowercase letters, return the minimum number of letters that must b ...
- Hibernate Tools插件的安装和使用
http://ricki.iteye.com/blog/842343 http://blog.csdn.net/kinmet2010/article/details/5976869 http://ww ...
- asp设置cookies过期时间
Response.Cookies("user_name").Expires=Date+1 '指定cookie保存时间 保留COOKIES一个小时 Response.Cookies( ...