Skip to content

Commit d026e85

Browse files
committed
feat: add solutions to lc problem: No.1506
No.1506.Find Root of N-Ary Tree
1 parent bf2c5b1 commit d026e85

File tree

7 files changed

+154
-95
lines changed

7 files changed

+154
-95
lines changed

solution/1500-1599/1506.Find Root of N-Ary Tree/README.md

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,15 @@ findRoot 函数应该返回根 Node(1) ,驱动程序代码将序列化它并
7474

7575
<!-- 这里可写通用的实现逻辑 -->
7676

77-
遍历 N 叉树 tree 的所有节点以及它们的子节点:
77+
**方法一:位运算**
7878

79-
- 对于非根节点,它会在 tree 列表中出现一次,并且在某个节点的 children 列表中出现一次,一共出现两次。
80-
- 对于根节点,它只会在 tree 列表中出现一次。
79+
对于一棵 N 叉树的节点,如果该节点是根节点,那么该节点只会出现一次在数组 `tree` 中;而如果该节点不是根节点,那么该节点会出现两次,一次在数组 `tree` 中,一次在该节点的父节点的 `children` 数组中。
8180

82-
我们对遍历到的节点及子节点进行按位异或运算,由于一个数异或两次等于没有进行任何运算,因此最后运算的结果就是根节点的值
81+
因此,我们可以遍历数组 `tree`,计算每个节点的值以及其所有子节点的值的异或和,记录在变量 $x$ 中。遍历结束后,我们得到的 $x$ 就是根节点的值
8382

84-
由于树中节点值唯一,我们再遍历一遍 tree 列表找出该节点即可。
83+
接下来,我们再遍历数组 `tree`,找到值为 $x$ 的节点,即为根节点,返回即可。
84+
85+
时间复杂度 $O(n)$,空间复杂度 $O(1)$。其中 $n$ 为数组 `tree` 的长度。
8586

8687
<!-- tabs:start -->
8788

@@ -101,15 +102,12 @@ class Node:
101102

102103
class Solution:
103104
def findRoot(self, tree: List['Node']) -> 'Node':
104-
xorsum = 0
105+
x = 0
105106
for node in tree:
106-
xorsum ^= node.val
107+
x ^= node.val
107108
for child in node.children:
108-
xorsum ^= child.val
109-
110-
for node in tree:
111-
if node.val == xorsum:
112-
return node
109+
x ^= child.val
110+
return next(node for node in tree if node.val == x)
113111
```
114112

115113
### **Java**
@@ -142,19 +140,18 @@ class Node {
142140

143141
class Solution {
144142
public Node findRoot(List<Node> tree) {
145-
int xor = 0;
143+
int x = 0;
146144
for (Node node : tree) {
147-
xor ^= node.val;
145+
x ^= node.val;
148146
for (Node child : node.children) {
149-
xor ^= child.val;
147+
x ^= child.val;
150148
}
151149
}
152-
for (Node node : tree) {
153-
if (node.val == xor) {
154-
return node;
150+
for (int i = 0; ; ++i) {
151+
if (tree.get(i).val == x) {
152+
return tree.get(i);
155153
}
156154
}
157-
return null;
158155
}
159156
}
160157
```
@@ -185,19 +182,18 @@ public:
185182
class Solution {
186183
public:
187184
Node* findRoot(vector<Node*> tree) {
188-
int xorsum = 0;
189-
for (auto& node : tree) {
190-
xorsum ^= node->val;
191-
for (auto& child : node->children) {
192-
xorsum ^= child->val;
185+
int x = 0;
186+
for (Node* node : tree) {
187+
x ^= node->val;
188+
for (Node* child : node->children) {
189+
x ^= child->val;
193190
}
194191
}
195-
for (auto& node : tree) {
196-
if (node->val == xorsum) {
197-
return node;
192+
for (int i = 0;; ++i) {
193+
if (tree[i]->val == x) {
194+
return tree[i];
198195
}
199196
}
200-
return nullptr;
201197
}
202198
};
203199
```
@@ -214,19 +210,45 @@ public:
214210
*/
215211
216212
func findRoot(tree []*Node) *Node {
217-
xorsum := 0
213+
x := 0
218214
for _, node := range tree {
219-
xorsum ^= node.Val
215+
x ^= node.Val
220216
for _, child := range node.Children {
221-
xorsum ^= child.Val
217+
x ^= child.Val
222218
}
223219
}
224-
for _, node := range tree {
225-
if node.Val == xorsum {
226-
return node
220+
for i := 0; ; i++ {
221+
if tree[i].Val == x {
222+
return tree[i]
227223
}
228224
}
229-
return nil
225+
}
226+
```
227+
228+
### **TypeScript**
229+
230+
```ts
231+
/**
232+
* Definition for Node.
233+
* class Node {
234+
* val: number
235+
* children: Node[]
236+
* constructor(val?: number, children?: Node[]) {
237+
* this.val = (val===undefined ? 0 : val)
238+
* this.children = (children===undefined ? [] : children)
239+
* }
240+
* }
241+
*/
242+
243+
function findRoot(tree: Node[]): Node | null {
244+
let x = 0;
245+
for (const node of tree) {
246+
x ^= node.val;
247+
for (const child of node.children) {
248+
x ^= child.val;
249+
}
250+
}
251+
return tree.find(node => node.val === x) || null;
230252
}
231253
```
232254

solution/1500-1599/1506.Find Root of N-Ary Tree/README_EN.md

Lines changed: 51 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -82,15 +82,12 @@ class Node:
8282

8383
class Solution:
8484
def findRoot(self, tree: List['Node']) -> 'Node':
85-
xorsum = 0
85+
x = 0
8686
for node in tree:
87-
xorsum ^= node.val
87+
x ^= node.val
8888
for child in node.children:
89-
xorsum ^= child.val
90-
91-
for node in tree:
92-
if node.val == xorsum:
93-
return node
89+
x ^= child.val
90+
return next(node for node in tree if node.val == x)
9491
```
9592

9693
### **Java**
@@ -121,19 +118,18 @@ class Node {
121118

122119
class Solution {
123120
public Node findRoot(List<Node> tree) {
124-
int xor = 0;
121+
int x = 0;
125122
for (Node node : tree) {
126-
xor ^= node.val;
123+
x ^= node.val;
127124
for (Node child : node.children) {
128-
xor ^= child.val;
125+
x ^= child.val;
129126
}
130127
}
131-
for (Node node : tree) {
132-
if (node.val == xor) {
133-
return node;
128+
for (int i = 0;; ++i) {
129+
if (tree.get(i).val == x) {
130+
return tree.get(i);
134131
}
135132
}
136-
return null;
137133
}
138134
}
139135
```
@@ -164,19 +160,18 @@ public:
164160
class Solution {
165161
public:
166162
Node* findRoot(vector<Node*> tree) {
167-
int xorsum = 0;
168-
for (auto& node : tree) {
169-
xorsum ^= node->val;
170-
for (auto& child : node->children) {
171-
xorsum ^= child->val;
163+
int x = 0;
164+
for (Node* node : tree) {
165+
x ^= node->val;
166+
for (Node* child : node->children) {
167+
x ^= child->val;
172168
}
173169
}
174-
for (auto& node : tree) {
175-
if (node->val == xorsum) {
176-
return node;
170+
for (int i = 0;; ++i) {
171+
if (tree[i]->val == x) {
172+
return tree[i];
177173
}
178174
}
179-
return nullptr;
180175
}
181176
};
182177
```
@@ -193,19 +188,45 @@ public:
193188
*/
194189
195190
func findRoot(tree []*Node) *Node {
196-
xorsum := 0
191+
x := 0
197192
for _, node := range tree {
198-
xorsum ^= node.Val
193+
x ^= node.Val
199194
for _, child := range node.Children {
200-
xorsum ^= child.Val
195+
x ^= child.Val
201196
}
202197
}
203-
for _, node := range tree {
204-
if node.Val == xorsum {
205-
return node
198+
for i := 0; ; i++ {
199+
if tree[i].Val == x {
200+
return tree[i]
206201
}
207202
}
208-
return nil
203+
}
204+
```
205+
206+
### **TypeScript**
207+
208+
```ts
209+
/**
210+
* Definition for Node.
211+
* class Node {
212+
* val: number
213+
* children: Node[]
214+
* constructor(val?: number, children?: Node[]) {
215+
* this.val = (val===undefined ? 0 : val)
216+
* this.children = (children===undefined ? [] : children)
217+
* }
218+
* }
219+
*/
220+
221+
function findRoot(tree: Node[]): Node | null {
222+
let x = 0;
223+
for (const node of tree) {
224+
x ^= node.val;
225+
for (const child of node.children) {
226+
x ^= child.val;
227+
}
228+
}
229+
return tree.find(node => node.val === x) || null;
209230
}
210231
```
211232

solution/1500-1599/1506.Find Root of N-Ary Tree/Solution.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@ class Node {
2121
class Solution {
2222
public:
2323
Node* findRoot(vector<Node*> tree) {
24-
int xorsum = 0;
25-
for (auto& node : tree) {
26-
xorsum ^= node->val;
27-
for (auto& child : node->children) {
28-
xorsum ^= child->val;
24+
int x = 0;
25+
for (Node* node : tree) {
26+
x ^= node->val;
27+
for (Node* child : node->children) {
28+
x ^= child->val;
2929
}
3030
}
31-
for (auto& node : tree) {
32-
if (node->val == xorsum) {
33-
return node;
31+
for (int i = 0;; ++i) {
32+
if (tree[i]->val == x) {
33+
return tree[i];
3434
}
3535
}
36-
return nullptr;
3736
}
3837
};

solution/1500-1599/1506.Find Root of N-Ary Tree/Solution.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,16 @@
77
*/
88

99
func findRoot(tree []*Node) *Node {
10-
xorsum := 0
10+
x := 0
1111
for _, node := range tree {
12-
xorsum ^= node.Val
12+
x ^= node.Val
1313
for _, child := range node.Children {
14-
xorsum ^= child.Val
14+
x ^= child.Val
1515
}
1616
}
17-
for _, node := range tree {
18-
if node.Val == xorsum {
19-
return node
17+
for i := 0; ; i++ {
18+
if tree[i].Val == x {
19+
return tree[i]
2020
}
2121
}
22-
return nil
2322
}

solution/1500-1599/1506.Find Root of N-Ary Tree/Solution.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@ public Node(int _val,ArrayList<Node> _children) {
2323

2424
class Solution {
2525
public Node findRoot(List<Node> tree) {
26-
int xor = 0;
26+
int x = 0;
2727
for (Node node : tree) {
28-
xor ^= node.val;
28+
x ^= node.val;
2929
for (Node child : node.children) {
30-
xor ^= child.val;
30+
x ^= child.val;
3131
}
3232
}
33-
for (Node node : tree) {
34-
if (node.val == xor) {
35-
return node;
33+
for (int i = 0;; ++i) {
34+
if (tree.get(i).val == x) {
35+
return tree.get(i);
3636
}
3737
}
38-
return null;
3938
}
4039
}

solution/1500-1599/1506.Find Root of N-Ary Tree/Solution.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,9 @@ def __init__(self, val=None, children=None):
99

1010
class Solution:
1111
def findRoot(self, tree: List['Node']) -> 'Node':
12-
xorsum = 0
12+
x = 0
1313
for node in tree:
14-
xorsum ^= node.val
14+
x ^= node.val
1515
for child in node.children:
16-
xorsum ^= child.val
17-
18-
for node in tree:
19-
if node.val == xorsum:
20-
return node
16+
x ^= child.val
17+
return next(node for node in tree if node.val == x)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Definition for Node.
3+
* class Node {
4+
* val: number
5+
* children: Node[]
6+
* constructor(val?: number, children?: Node[]) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.children = (children===undefined ? [] : children)
9+
* }
10+
* }
11+
*/
12+
13+
function findRoot(tree: Node[]): Node | null {
14+
let x = 0;
15+
for (const node of tree) {
16+
x ^= node.val;
17+
for (const child of node.children) {
18+
x ^= child.val;
19+
}
20+
}
21+
return tree.find(node => node.val === x) || null;
22+
}

0 commit comments

Comments
 (0)