算法:求树的最大深度104. Maximum Depth of Binary Tree
LeetCode全集请参考:LeetCode Github 大全
题目
104. Maximum Depth of Binary Tree
Given the root of a binary tree, return its maximum depth.
A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 3
Example 2:
Input: root = [1,null,2]
Output: 2
Example 3:
Input: root = []
Output: 0
Example 4:
Input: root = [0]
Output: 1
Constraints:
The number of nodes in the tree is in the range [0, 104].
-100 <= Node.val <= 100
深度优先算法
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public int maxDepth(TreeNode root) {// check edgeif (root == null) {return 0;}return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));}
}
算法:求树的最大深度104. Maximum Depth of Binary Tree相关推荐
- 【LeetCode 剑指offer刷题】树题4:104 Maximum Depth of Binary Tree
[LeetCode & 剑指offer 刷题笔记]目录(持续更新中...) 104. Maximum Depth of Binary Tree Given a binary tree, fin ...
- LC 104. Maximum Depth of Binary Tree
1.题意 104. Maximum Depth of Binary Tree Easy 98540 Given a binary tree, find its maximum depth. The m ...
- 【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 ...
- C#LeetCode刷题之#104-二叉树的最大深度(Maximum Depth of Binary Tree)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4072 访问. 给定一个二叉树,找出其最大深度. 二叉树的深度为根 ...
- leetcode 104. 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 python3 简单题104. Maximum Depth of Binary Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零四题 (1)题目 英文: Given a binary tree, find ...
- 104. Maximum Depth of Binary Tree (Easy)
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
题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the l ...
- Leet Code OJ 104. Maximum Depth of Binary Tree [Difficulty: Easy]
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- 【leetcode】104. Maximum Depth of Binary Tree
1. 题目 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along th ...
最新文章
- 翡润年华-毛货展示003
- js中常用的算法排序
- 机器人焊枪动作与编程实验_机器人编程实验报告.pdf
- 从零开始学python项目_从零开始可以这样学python
- 1-4flink概述
- POJ NOI MATH-7652 乘积最大的拆分
- git理解HEAD,master
- 自适应漫画手机APP应用下载页源码
- [渝粤教育] 西南科技大学 交通管理与控制 在线考试复习资料2021版
- linux服务器怎么拷贝文件,linux 服务器之间拷贝文件
- xshell上传文件插件lrzsz
- linux ubi 分区,ubi卷重分区及卷创建流程解析
- Hadoop3+Hive3安装记录(虚拟机搭建分布式环境,报错解决)
- idea创建的maven web工程如何修改web版本
- ASCII、Unicode、UTF-8 和 Base64 等编码
- vivo8.0系统机器最完美激活xposed框架的方法
- (logN)²是O(N)的
- 10 款免费 PDF 阅读器软件工具分享
- 2023第八届少儿模特明星盛典 小超模矫沐含 担任全球赛代言人
- 75道经典AI面试题,我就想把你们安排的明明白白的!(含答案)