File tree 7 files changed +145
-39
lines changed
0300-0399/0356.Line Reflection
0500-0599/0559.Maximum Depth of N-ary Tree
7 files changed +145
-39
lines changed Original file line number Diff line number Diff line change @@ -60,13 +60,13 @@ class Solution:
60
60
def isReflected (self , points : List[List[int ]]) -> bool :
61
61
min_x, max_x = float (' inf' ), float (' -inf' )
62
62
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 ))
67
67
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:
70
70
return False
71
71
return True
72
72
```
Original file line number Diff line number Diff line change @@ -49,13 +49,13 @@ class Solution:
49
49
def isReflected (self , points : List[List[int ]]) -> bool :
50
50
min_x, max_x = float (' inf' ), float (' -inf' )
51
51
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 ))
56
56
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:
59
59
return False
60
60
return True
61
61
```
Original file line number Diff line number Diff line change @@ -2,12 +2,12 @@ class Solution:
2
2
def isReflected (self , points : List [List [int ]]) -> bool :
3
3
min_x , max_x = float ('inf' ), float ('-inf' )
4
4
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 ))
9
9
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 :
12
12
return False
13
13
return True
Original file line number Diff line number Diff line change 41
41
<li>树的节点数目位于 <code>[0, 10<sup>4</sup>]</code> 之间。</li>
42
42
</ul >
43
43
44
-
45
44
## 解法
46
45
47
46
<!-- 这里可写通用的实现逻辑 -->
53
52
<!-- 这里可写当前语言的特殊实现逻辑 -->
54
53
55
54
``` 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
57
71
```
58
72
59
73
### ** Java**
60
74
61
75
<!-- 这里可写当前语言的特殊实现逻辑 -->
62
76
63
77
``` 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
+ }
65
109
```
66
110
67
111
### ** ...**
Original file line number Diff line number Diff line change 6
6
7
7
<p >Given a n-ary tree, find its maximum depth.</p >
8
8
9
-
10
-
11
9
<p >The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.</p >
12
10
13
-
14
-
15
11
<p ><em >Nary-Tree input serialization  ; is represented in their level order traversal, each group of children is separated by the null value (See examples).</em ></p >
16
12
17
-
18
13
<p >  ; </p >
19
14
<p ><strong >Example 1:</strong ></p >
20
15
42
37
<li>The total number of nodes is between <code>[0, 10<sup>4</sup>]</code>.</li>
43
38
</ul >
44
39
45
-
46
40
## Solutions
47
41
48
42
<!-- tabs:start -->
49
43
50
44
### ** Python3**
51
45
52
46
``` 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
54
63
```
55
64
56
65
### ** Java**
57
66
58
67
``` 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
+ }
60
99
```
61
100
62
101
### ** ...**
Original file line number Diff line number Diff line change
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 number Diff line number Diff line change 1
-
2
-
3
1
"""
4
2
# Definition for a Node.
5
3
class Node:
6
4
def __init__(self, val=None, children=None):
7
5
self.val = val
8
6
self.children = children
9
7
"""
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
+
15
9
class Solution :
16
10
def maxDepth (self , root : 'Node' ) -> int :
17
11
if not root :
18
12
return 0
19
13
max_depth = 1
20
14
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 ))
23
16
return max_depth
24
-
You can’t perform that action at this time.
0 commit comments