Skip to content

Commit 4402929

Browse files
committed
feat: update solutions to lc problem: No.1490
No.1490.Clone N-ary Tree
1 parent 40c7e69 commit 4402929

File tree

10 files changed

+73
-61
lines changed

10 files changed

+73
-61
lines changed

solution/1400-1499/1486.XOR Operation in an Array/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class Solution:
7575
return ans
7676
```
7777

78+
```python
79+
class Solution:
80+
def xorOperation(self, n: int, start: int) -> int:
81+
return reduce(xor, ((start + 2 * i) for i in range(n)))
82+
```
83+
7884
### **Java**
7985

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

solution/1400-1499/1486.XOR Operation in an Array/README_EN.md

+6
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ class Solution:
5252
return ans
5353
```
5454

55+
```python
56+
class Solution:
57+
def xorOperation(self, n: int, start: int) -> int:
58+
return reduce(xor, ((start + 2 * i) for i in range(n)))
59+
```
60+
5561
### **Java**
5662

5763
```java

solution/1400-1499/1490.Clone N-ary Tree/README.md

+22-18
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,13 @@ class Node {
5656

5757
<!-- 这里可写通用的实现逻辑 -->
5858

59-
DFS。
59+
**方法一:递归**
60+
61+
我们可以用递归的方法来实现 N 叉树的深拷贝。
62+
63+
对于当前节点,如果为空,则返回空;否则,创建一个新节点,其值为当前节点的值,然后对当前节点的每个子节点递归调用该函数,将返回值作为新节点的子节点。最后返回新节点即可。
64+
65+
时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为 N 叉树的节点个数。
6066

6167
<!-- tabs:start -->
6268

@@ -76,10 +82,10 @@ class Node:
7682

7783
class Solution:
7884
def cloneTree(self, root: 'Node') -> 'Node':
79-
if root:
80-
node = Node(val=root.val)
81-
node.children = [self.cloneTree(child) for child in root.children]
82-
return node
85+
if root is None:
86+
return None
87+
children = [self.cloneTree(child) for child in root.children]
88+
return Node(root.val, children)
8389
```
8490

8591
### **Java**
@@ -115,11 +121,11 @@ class Solution {
115121
if (root == null) {
116122
return null;
117123
}
118-
Node node = new Node(root.val);
124+
ArrayList<Node> children = new ArrayList<>();
119125
for (Node child : root.children) {
120-
node.children.add(cloneTree(child));
126+
children.add(cloneTree(child));
121127
}
122-
return node;
128+
return new Node(root.val, children);
123129
}
124130
}
125131
```
@@ -150,16 +156,14 @@ public:
150156
class Solution {
151157
public:
152158
Node* cloneTree(Node* root) {
153-
if (root == nullptr) {
154-
return nullptr;
159+
if (!root) {
160+
return root;
155161
}
156-
Node* node = new Node(root->val);
157162
vector<Node*> children;
158-
for (Node* node : root->children) {
159-
children.push_back(cloneTree(node));
163+
for (Node* child : root->children) {
164+
children.emplace_back(cloneTree(child));
160165
}
161-
node->children = children;
162-
return node;
166+
return new Node(root->val, children);
163167
}
164168
};
165169
```
@@ -179,11 +183,11 @@ func cloneTree(root *Node) *Node {
179183
if root == nil {
180184
return nil
181185
}
182-
node := &Node{Val: root.Val}
186+
children := []*Node{}
183187
for _, child := range root.Children {
184-
node.Children = append(node.Children, cloneTree(child))
188+
children = append(children, cloneTree(child))
185189
}
186-
return node
190+
return &Node{root.Val, children}
187191
}
188192
```
189193

solution/1400-1499/1490.Clone N-ary Tree/README_EN.md

+15-17
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ class Node:
6767

6868
class Solution:
6969
def cloneTree(self, root: 'Node') -> 'Node':
70-
if root:
71-
node = Node(val=root.val)
72-
node.children = [self.cloneTree(child) for child in root.children]
73-
return node
70+
if root is None:
71+
return None
72+
children = [self.cloneTree(child) for child in root.children]
73+
return Node(root.val, children)
7474
```
7575

7676
### **Java**
@@ -104,11 +104,11 @@ class Solution {
104104
if (root == null) {
105105
return null;
106106
}
107-
Node node = new Node(root.val);
107+
ArrayList<Node> children = new ArrayList<>();
108108
for (Node child : root.children) {
109-
node.children.add(cloneTree(child));
109+
children.add(cloneTree(child));
110110
}
111-
return node;
111+
return new Node(root.val, children);
112112
}
113113
}
114114
```
@@ -139,16 +139,14 @@ public:
139139
class Solution {
140140
public:
141141
Node* cloneTree(Node* root) {
142-
if (root == nullptr) {
143-
return nullptr;
142+
if (!root) {
143+
return root;
144144
}
145-
Node* node = new Node(root->val);
146145
vector<Node*> children;
147-
for (Node* node : root->children) {
148-
children.push_back(cloneTree(node));
146+
for (Node* child : root->children) {
147+
children.emplace_back(cloneTree(child));
149148
}
150-
node->children = children;
151-
return node;
149+
return new Node(root->val, children);
152150
}
153151
};
154152
```
@@ -168,11 +166,11 @@ func cloneTree(root *Node) *Node {
168166
if root == nil {
169167
return nil
170168
}
171-
node := &Node{Val: root.Val}
169+
children := []*Node{}
172170
for _, child := range root.Children {
173-
node.Children = append(node.Children, cloneTree(child))
171+
children = append(children, cloneTree(child))
174172
}
175-
return node
173+
return &Node{root.Val, children}
176174
}
177175
```
178176

solution/1400-1499/1490.Clone N-ary Tree/Solution.cpp

+5-7
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ class Node {
2121
class Solution {
2222
public:
2323
Node* cloneTree(Node* root) {
24-
if (root == nullptr) {
25-
return nullptr;
24+
if (!root) {
25+
return root;
2626
}
27-
Node* node = new Node(root->val);
2827
vector<Node*> children;
29-
for (Node* node : root->children) {
30-
children.push_back(cloneTree(node));
28+
for (Node* child : root->children) {
29+
children.emplace_back(cloneTree(child));
3130
}
32-
node->children = children;
33-
return node;
31+
return new Node(root->val, children);
3432
}
3533
};

solution/1400-1499/1490.Clone N-ary Tree/Solution.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ func cloneTree(root *Node) *Node {
1010
if root == nil {
1111
return nil
1212
}
13-
node := &Node{Val: root.Val}
13+
children := []*Node{}
1414
for _, child := range root.Children {
15-
node.Children = append(node.Children, cloneTree(child))
15+
children = append(children, cloneTree(child))
1616
}
17-
return node
17+
return &Node{root.Val, children}
1818
}

solution/1400-1499/1490.Clone N-ary Tree/Solution.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ class Node {
44
public int val;
55
public List<Node> children;
66
7-
7+
88
public Node() {
99
children = new ArrayList<Node>();
1010
}
11-
11+
1212
public Node(int _val) {
1313
val = _val;
1414
children = new ArrayList<Node>();
1515
}
16-
16+
1717
public Node(int _val,ArrayList<Node> _children) {
1818
val = _val;
1919
children = _children;
@@ -26,10 +26,10 @@ public Node cloneTree(Node root) {
2626
if (root == null) {
2727
return null;
2828
}
29-
Node node = new Node(root.val);
29+
ArrayList<Node> children = new ArrayList<>();
3030
for (Node child : root.children) {
31-
node.children.add(cloneTree(child));
31+
children.add(cloneTree(child));
3232
}
33-
return node;
33+
return new Node(root.val, children);
3434
}
3535
}

solution/1400-1499/1490.Clone N-ary Tree/Solution.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def __init__(self, val=None, children=None):
99

1010
class Solution:
1111
def cloneTree(self, root: 'Node') -> 'Node':
12-
if root:
13-
node = Node(val=root.val)
14-
node.children = [self.cloneTree(child) for child in root.children]
15-
return node
12+
if root is None:
13+
return None
14+
children = [self.cloneTree(child) for child in root.children]
15+
return Node(root.val, children)

solution/1500-1599/1510.Stone Game IV/README_EN.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,12 @@ class Solution:
8585
```java
8686
class Solution {
8787
private Boolean[] f;
88-
88+
8989
public boolean winnerSquareGame(int n) {
9090
f = new Boolean[n + 1];
9191
return dfs(n);
9292
}
93-
93+
9494
private boolean dfs(int i) {
9595
if (i <= 0) {
9696
return false;

solution/1800-1899/1845.Seat Reservation Manager/README_EN.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ class SeatManager {
8484
q.offer(i);
8585
}
8686
}
87-
87+
8888
public int reserve() {
8989
return q.poll();
9090
}
91-
91+
9292
public void unreserve(int seatNumber) {
9393
q.offer(seatNumber);
9494
}
@@ -112,13 +112,13 @@ public:
112112
q.push(i);
113113
}
114114
}
115-
115+
116116
int reserve() {
117117
int seat = q.top();
118118
q.pop();
119119
return seat;
120120
}
121-
121+
122122
void unreserve(int seatNumber) {
123123
q.push(seatNumber);
124124
}

0 commit comments

Comments
 (0)