Skip to content

Commit 30981f0

Browse files
committed
feat: add solutions to lc problem: No.0559. Maximum Depth of N-ary Tree
1 parent 0483eaa commit 30981f0

File tree

7 files changed

+145
-39
lines changed

7 files changed

+145
-39
lines changed

solution/0300-0399/0356.Line Reflection/README.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ class Solution:
6060
def isReflected(self, points: List[List[int]]) -> bool:
6161
min_x, max_x = float('inf'), float('-inf')
6262
point_set = set()
63-
for point in points:
64-
min_x = min(min_x, point[0])
65-
max_x = max(max_x, point[0])
66-
point_set.add((point[0], point[1]))
63+
for x, y in points:
64+
min_x = min(min_x, x)
65+
max_x = max(max_x, x)
66+
point_set.add((x, y))
6767
s = min_x + max_x
68-
for point in points:
69-
if (s - point[0], point[1]) not in point_set:
68+
for x, y in points:
69+
if (s - x, y) not in point_set:
7070
return False
7171
return True
7272
```

solution/0300-0399/0356.Line Reflection/README_EN.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,13 @@ class Solution:
4949
def isReflected(self, points: List[List[int]]) -> bool:
5050
min_x, max_x = float('inf'), float('-inf')
5151
point_set = set()
52-
for point in points:
53-
min_x = min(min_x, point[0])
54-
max_x = max(max_x, point[0])
55-
point_set.add((point[0], point[1]))
52+
for x, y in points:
53+
min_x = min(min_x, x)
54+
max_x = max(max_x, x)
55+
point_set.add((x, y))
5656
s = min_x + max_x
57-
for point in points:
58-
if (s - point[0], point[1]) not in point_set:
57+
for x, y in points:
58+
if (s - x, y) not in point_set:
5959
return False
6060
return True
6161
```

solution/0300-0399/0356.Line Reflection/Solution.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ class Solution:
22
def isReflected(self, points: List[List[int]]) -> bool:
33
min_x, max_x = float('inf'), float('-inf')
44
point_set = set()
5-
for point in points:
6-
min_x = min(min_x, point[0])
7-
max_x = max(max_x, point[0])
8-
point_set.add((point[0], point[1]))
5+
for x, y in points:
6+
min_x = min(min_x, x)
7+
max_x = max(max_x, x)
8+
point_set.add((x, y))
99
s = min_x + max_x
10-
for point in points:
11-
if (s - point[0], point[1]) not in point_set:
10+
for x, y in points:
11+
if (s - x, y) not in point_set:
1212
return False
1313
return True

solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md

+47-3
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
<li>树的节点数目位于 <code>[0, 10<sup>4</sup>]</code> 之间。</li>
4242
</ul>
4343

44-
4544
## 解法
4645

4746
<!-- 这里可写通用的实现逻辑 -->
@@ -53,15 +52,60 @@
5352
<!-- 这里可写当前语言的特殊实现逻辑 -->
5453

5554
```python
56-
55+
"""
56+
# Definition for a Node.
57+
class Node:
58+
def __init__(self, val=None, children=None):
59+
self.val = val
60+
self.children = children
61+
"""
62+
63+
class Solution:
64+
def maxDepth(self, root: 'Node') -> int:
65+
if not root:
66+
return 0
67+
max_depth = 1
68+
for child in root.children:
69+
max_depth = max(max_depth, 1 + self.maxDepth(child))
70+
return max_depth
5771
```
5872

5973
### **Java**
6074

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

6377
```java
64-
78+
/*
79+
// Definition for a Node.
80+
class Node {
81+
public int val;
82+
public List<Node> children;
83+
84+
public Node() {}
85+
86+
public Node(int _val) {
87+
val = _val;
88+
}
89+
90+
public Node(int _val, List<Node> _children) {
91+
val = _val;
92+
children = _children;
93+
}
94+
};
95+
*/
96+
97+
class Solution {
98+
public int maxDepth(Node root) {
99+
if (root == null) {
100+
return 0;
101+
}
102+
int maxDepth = 1;
103+
for (Node child : root.children) {
104+
maxDepth = Math.max(maxDepth, 1 + maxDepth(child));
105+
}
106+
return maxDepth;
107+
}
108+
}
65109
```
66110

67111
### **...**

solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md

+47-8
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,10 @@
66

77
<p>Given a n-ary tree, find its maximum depth.</p>
88

9-
10-
119
<p>The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.</p>
1210

13-
14-
1511
<p><em>Nary-Tree input serialization&nbsp;is represented in their level order traversal, each group of children is separated by the null value (See examples).</em></p>
1612

17-
1813
<p>&nbsp;</p>
1914
<p><strong>Example 1:</strong></p>
2015

@@ -42,21 +37,65 @@
4237
<li>The total number of nodes is between <code>[0,&nbsp;10<sup>4</sup>]</code>.</li>
4338
</ul>
4439

45-
4640
## Solutions
4741

4842
<!-- tabs:start -->
4943

5044
### **Python3**
5145

5246
```python
53-
47+
"""
48+
# Definition for a Node.
49+
class Node:
50+
def __init__(self, val=None, children=None):
51+
self.val = val
52+
self.children = children
53+
"""
54+
55+
class Solution:
56+
def maxDepth(self, root: 'Node') -> int:
57+
if not root:
58+
return 0
59+
max_depth = 1
60+
for child in root.children:
61+
max_depth = max(max_depth, 1 + self.maxDepth(child))
62+
return max_depth
5463
```
5564

5665
### **Java**
5766

5867
```java
59-
68+
/*
69+
// Definition for a Node.
70+
class Node {
71+
public int val;
72+
public List<Node> children;
73+
74+
public Node() {}
75+
76+
public Node(int _val) {
77+
val = _val;
78+
}
79+
80+
public Node(int _val, List<Node> _children) {
81+
val = _val;
82+
children = _children;
83+
}
84+
};
85+
*/
86+
87+
class Solution {
88+
public int maxDepth(Node root) {
89+
if (root == null) {
90+
return 0;
91+
}
92+
int maxDepth = 1;
93+
for (Node child : root.children) {
94+
maxDepth = Math.max(maxDepth, 1 + maxDepth(child));
95+
}
96+
return maxDepth;
97+
}
98+
}
6099
```
61100

62101
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public int val;
5+
public List<Node> children;
6+
7+
public Node() {}
8+
9+
public Node(int _val) {
10+
val = _val;
11+
}
12+
13+
public Node(int _val, List<Node> _children) {
14+
val = _val;
15+
children = _children;
16+
}
17+
};
18+
*/
19+
20+
class Solution {
21+
public int maxDepth(Node root) {
22+
if (root == null) {
23+
return 0;
24+
}
25+
int maxDepth = 1;
26+
for (Node child : root.children) {
27+
maxDepth = Math.max(maxDepth, 1 + maxDepth(child));
28+
}
29+
return maxDepth;
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
1-
2-
31
"""
42
# Definition for a Node.
53
class Node:
64
def __init__(self, val=None, children=None):
75
self.val = val
86
self.children = children
97
"""
10-
# Performance
11-
'''
12-
Runtime: 36 ms, faster than 99.57% of Python3 online submissions for Maximum Depth of N-ary Tree.
13-
Memory Usage: 14.5 MB, less than 100.00% of Python3 online submissions for Maximum Depth of N-ary Tree.
14-
'''
8+
159
class Solution:
1610
def maxDepth(self, root: 'Node') -> int:
1711
if not root:
1812
return 0
1913
max_depth = 1
2014
for child in root.children:
21-
max_depth = max(self.maxDepth(child) + 1 , max_depth)
22-
#print(max_depth, root.val)
15+
max_depth = max(max_depth, 1 + self.maxDepth(child))
2316
return max_depth
24-

0 commit comments

Comments
 (0)