Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3f01c24

Browse files
committed
solve 104.二叉树的最大深度
1 parent c1fbea5 commit 3f01c24

File tree

2 files changed

+121
-0
lines changed

2 files changed

+121
-0
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* @lc app=leetcode.cn id=104 lang=java
3+
*
4+
* [104] 二叉树的最大深度
5+
*
6+
* https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
7+
*
8+
* algorithms
9+
* Easy (73.22%)
10+
* Likes: 568
11+
* Dislikes: 0
12+
* Total Accepted: 191.6K
13+
* Total Submissions: 260.8K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给定一个二叉树,找出其最大深度。
17+
*
18+
* 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
19+
*
20+
* 说明: 叶子节点是指没有子节点的节点。
21+
*
22+
* 示例:
23+
* 给定二叉树 [3,9,20,null,null,15,7],
24+
*
25+
* ⁠ 3
26+
* ⁠ / \
27+
* ⁠ 9 20
28+
* ⁠ / \
29+
* ⁠ 15 7
30+
*
31+
* 返回它的最大深度 3 。
32+
*
33+
*/
34+
35+
// @lc code=start
36+
/**
37+
* Definition for a binary tree node.
38+
* public class TreeNode {
39+
* int val;
40+
* TreeNode left;
41+
* TreeNode right;
42+
* TreeNode(int x) { val = x; }
43+
* }
44+
*/
45+
class Solution {
46+
int max = 0;
47+
48+
public int maxDepth(TreeNode root) {
49+
maxDepth(root, 1);
50+
return max;
51+
}
52+
53+
public void maxDepth(TreeNode root, int depth) {
54+
if (root == null) {
55+
return;
56+
}
57+
if (root.left == null && root.right == null) {
58+
max = Math.max(max, depth);
59+
}
60+
maxDepth(root.left, depth + 1);
61+
maxDepth(root.right, depth + 1);
62+
}
63+
}
64+
// @lc code=end
65+

‎zh/104.二叉树的最大深度.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
* @lc app=leetcode.cn id=104 lang=java
3+
*
4+
* [104] 二叉树的最大深度
5+
*
6+
* https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/description/
7+
*
8+
* algorithms
9+
* Easy (73.22%)
10+
* Likes: 568
11+
* Dislikes: 0
12+
* Total Accepted: 191.6K
13+
* Total Submissions: 260.8K
14+
* Testcase Example: '[3,9,20,null,null,15,7]'
15+
*
16+
* 给定一个二叉树,找出其最大深度。
17+
*
18+
* 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
19+
*
20+
* 说明: 叶子节点是指没有子节点的节点。
21+
*
22+
* 示例:
23+
* 给定二叉树 [3,9,20,null,null,15,7],
24+
*
25+
* ⁠ 3
26+
* ⁠ / \
27+
* ⁠ 9 20
28+
* ⁠ / \
29+
* ⁠ 15 7
30+
*
31+
* 返回它的最大深度 3 。
32+
*
33+
*/
34+
35+
// @lc code=start
36+
/**
37+
* Definition for a binary tree node.
38+
* public class TreeNode {
39+
* int val;
40+
* TreeNode left;
41+
* TreeNode right;
42+
* TreeNode(int x) { val = x; }
43+
* }
44+
*/
45+
class Solution {
46+
public int maxDepth(TreeNode root) {
47+
if (root == null) {
48+
return 0;
49+
}
50+
int left = maxDepth(root.left);
51+
int right = maxDepth(root.right);
52+
return Math.max(left, right) + 1;
53+
}
54+
}
55+
// @lc code=end
56+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /