Skip to content

Commit 7470864

Browse files
committed
feat: add python and java solutions to leetcode problem: No.0104
See https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
1 parent 6169b5e commit 7470864

File tree

6 files changed

+130
-17
lines changed

6 files changed

+130
-17
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@
8181

8282
### 二叉树
8383

84+
1. [二叉树的最大深度](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README.md)
8485
1. [二叉树的最近公共祖先](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README.md)
8586
1. [二叉搜索树的最近公共祖先](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README.md)
8687

README_EN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Complete solutions to [LeetCode](https://leetcode-cn.com/problemset/all/), [LCOF
7979

8080
### Binary Tree
8181

82+
1. [Maximum Depth of Binary Tree](/solution/0100-0199/0104.Maximum%20Depth%20of%20Binary%20Tree/README_EN.md)
8283
1. [Lowest Common Ancestor of a Binary Tree](/solution/0200-0299/0236.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Tree/README_EN.md)
8384
1. [Lowest Common Ancestor of a Binary Search Tree](/solution/0200-0299/0235.Lowest%20Common%20Ancestor%20of%20a%20Binary%20Search%20Tree/README_EN.md)
8485

solution/0100-0199/0104.Maximum Depth of Binary Tree/README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,72 @@
2626

2727
<!-- 这里可写通用的实现逻辑 -->
2828

29+
递归遍历左右子树,求左右子树的最大深度 +1 即可。
30+
2931
<!-- tabs:start -->
3032

3133
### **Python3**
3234

3335
<!-- 这里可写当前语言的特殊实现逻辑 -->
3436

3537
```python
36-
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, val=0, left=None, right=None):
41+
# self.val = val
42+
# self.left = left
43+
# self.right = right
44+
class Solution:
45+
def maxDepth(self, root: TreeNode) -> int:
46+
if root is None:
47+
return 0
48+
l = self.maxDepth(root.left)
49+
r = self.maxDepth(root.right)
50+
return 1 + max(l, r)
3751
```
3852

3953
### **Java**
4054

4155
<!-- 这里可写当前语言的特殊实现逻辑 -->
4256

4357
```java
58+
/**
59+
* Definition for a binary tree node.
60+
* public class TreeNode {
61+
* int val;
62+
* TreeNode left;
63+
* TreeNode right;
64+
* TreeNode() {}
65+
* TreeNode(int val) { this.val = val; }
66+
* TreeNode(int val, TreeNode left, TreeNode right) {
67+
* this.val = val;
68+
* this.left = left;
69+
* this.right = right;
70+
* }
71+
* }
72+
*/
73+
class Solution {
74+
public int maxDepth(TreeNode root) {
75+
if (root == null) return 0;
76+
int l = maxDepth(root.left);
77+
int r = maxDepth(root.right);
78+
return 1 + Math.max(l, r);
79+
}
80+
}
81+
```
4482

83+
### **C++**
84+
85+
```cpp
86+
class Solution {
87+
public:
88+
int maxDepth(TreeNode* root) {
89+
if (!root) return 0;
90+
int l = maxDepth(root->left);
91+
int r = maxDepth(root->right);
92+
return max(l, r) + 1;
93+
}
94+
};
4595
```
4696
4797
### **...**

solution/0100-0199/0104.Maximum Depth of Binary Tree/README_EN.md

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,61 @@
3535
### **Python3**
3636

3737
```python
38-
38+
# Definition for a binary tree node.
39+
# class TreeNode:
40+
# def __init__(self, val=0, left=None, right=None):
41+
# self.val = val
42+
# self.left = left
43+
# self.right = right
44+
class Solution:
45+
def maxDepth(self, root: TreeNode) -> int:
46+
if root is None:
47+
return 0
48+
l = self.maxDepth(root.left)
49+
r = self.maxDepth(root.right)
50+
return 1 + max(l, r)
3951
```
4052

4153
### **Java**
4254

4355
```java
56+
/**
57+
* Definition for a binary tree node.
58+
* public class TreeNode {
59+
* int val;
60+
* TreeNode left;
61+
* TreeNode right;
62+
* TreeNode() {}
63+
* TreeNode(int val) { this.val = val; }
64+
* TreeNode(int val, TreeNode left, TreeNode right) {
65+
* this.val = val;
66+
* this.left = left;
67+
* this.right = right;
68+
* }
69+
* }
70+
*/
71+
class Solution {
72+
public int maxDepth(TreeNode root) {
73+
if (root == null) return 0;
74+
int l = maxDepth(root.left);
75+
int r = maxDepth(root.right);
76+
return 1 + Math.max(l, r);
77+
}
78+
}
79+
```
4480

81+
### **C++**
82+
83+
```cpp
84+
class Solution {
85+
public:
86+
int maxDepth(TreeNode* root) {
87+
if (!root) return 0;
88+
int l = maxDepth(root->left);
89+
int r = maxDepth(root->right);
90+
return max(l, r) + 1;
91+
}
92+
};
4593
```
4694
4795
### **...**
Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
116
class Solution {
217
public int maxDepth(TreeNode root) {
318
if (root == null) return 0;
4-
return Integer.max(maxDepth(root.left), maxDepth(root.right)) + 1;
19+
int l = maxDepth(root.left);
20+
int r = maxDepth(root.right);
21+
return 1 + Math.max(l, r);
522
}
623
}
Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
# Definition for a binary tree node.
2-
# class TreeNode(object):
3-
# def __init__(self, x):
4-
# self.val = x
5-
# self.left = None
6-
# self.right = None
7-
8-
9-
class Solution(object):
10-
def maxDepth(self, root):
11-
"""
12-
:type root: TreeNode
13-
:rtype: int
14-
"""
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def maxDepth(self, root: TreeNode) -> int:
159
if root is None:
1610
return 0
17-
return 1 + max(self.maxDepth(root.left), self.maxDepth(root.right))
11+
l = self.maxDepth(root.left)
12+
r = self.maxDepth(root.right)
13+
return 1 + max(l, r)

0 commit comments

Comments
 (0)