Skip to content

Commit 7c61caa

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

File tree

6 files changed

+387
-4
lines changed

6 files changed

+387
-4
lines changed

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

+138-2
Original file line numberDiff line numberDiff line change
@@ -70,27 +70,163 @@ findRoot 函数应该返回根 Node(1) ,驱动程序代码将序列化它并
7070
<li>你可以使用 O(1) 额外内存空间且 O(n) 时间复杂度的算法来找到该树的根节点吗?</li>
7171
</ul>
7272

73-
7473
## 解法
7574

7675
<!-- 这里可写通用的实现逻辑 -->
7776

77+
遍历 N 叉树 tree 的所有节点以及它们的子节点:
78+
79+
- 对于非根节点,它会在 tree 列表中出现一次,并且在某个节点的 children 列表中出现一次,一共出现两次。
80+
- 对于根节点,它只会在 tree 列表中出现一次。
81+
82+
我们对遍历到的节点及子节点进行按位异或运算,由于一个数异或两次等于没有进行任何运算,因此最后运算的结果就是根节点的值。
83+
84+
由于树中节点值唯一,我们再遍历一遍 tree 列表找出该节点即可。
85+
7886
<!-- tabs:start -->
7987

8088
### **Python3**
8189

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

8492
```python
85-
93+
"""
94+
# Definition for a Node.
95+
class Node:
96+
def __init__(self, val=None, children=None):
97+
self.val = val
98+
self.children = children if children is not None else []
99+
"""
100+
101+
class Solution:
102+
def findRoot(self, tree: List['Node']) -> 'Node':
103+
xorsum = 0
104+
for node in tree:
105+
xorsum ^= node.val
106+
for child in node.children:
107+
xorsum ^= child.val
108+
109+
for node in tree:
110+
if node.val == xorsum:
111+
return node
86112
```
87113

88114
### **Java**
89115

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

92118
```java
119+
/*
120+
// Definition for a Node.
121+
class Node {
122+
public int val;
123+
public List<Node> children;
124+
125+
126+
public Node() {
127+
children = new ArrayList<Node>();
128+
}
129+
130+
public Node(int _val) {
131+
val = _val;
132+
children = new ArrayList<Node>();
133+
}
134+
135+
public Node(int _val,ArrayList<Node> _children) {
136+
val = _val;
137+
children = _children;
138+
}
139+
};
140+
*/
141+
142+
class Solution {
143+
public Node findRoot(List<Node> tree) {
144+
int xor = 0;
145+
for (Node node : tree) {
146+
xor ^= node.val;
147+
for (Node child : node.children) {
148+
xor ^= child.val;
149+
}
150+
}
151+
for (Node node :tree) {
152+
if (node.val == xor) {
153+
return node;
154+
}
155+
}
156+
return null;
157+
}
158+
}
159+
```
160+
161+
### **C++**
162+
163+
```cpp
164+
/*
165+
// Definition for a Node.
166+
class Node {
167+
public:
168+
int val;
169+
vector<Node*> children;
170+
171+
Node() {}
172+
173+
Node(int _val) {
174+
val = _val;
175+
}
176+
177+
Node(int _val, vector<Node*> _children) {
178+
val = _val;
179+
children = _children;
180+
}
181+
};
182+
*/
183+
184+
class Solution {
185+
public:
186+
Node* findRoot(vector<Node*> tree) {
187+
int xorsum = 0;
188+
for (auto& node : tree) {
189+
xorsum ^= node->val;
190+
for (auto& child : node->children) {
191+
xorsum ^= child->val;
192+
}
193+
}
194+
for (auto& node : tree) {
195+
if (node->val == xorsum) {
196+
return node;
197+
}
198+
}
199+
return nullptr;
200+
}
201+
};
202+
```
93203
204+
### **Go**
205+
206+
```go
207+
/**
208+
* Definition for a Node.
209+
* type Node struct {
210+
* Val int
211+
* Children []*Node
212+
* }
213+
*/
214+
215+
func findRoot(tree []*Node) *Node {
216+
xorsum := 0
217+
for _, node := range tree {
218+
xorsum ^= node.Val
219+
for _, child := range node.Children {
220+
xorsum ^= child.Val
221+
}
222+
}
223+
for _, node := range tree {
224+
if node.Val == xorsum {
225+
return node
226+
}
227+
}
228+
return nil
229+
}
94230
```
95231

96232
### **...**

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

+129-2
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,148 @@ The input data and serialized Node(1) are the same, so the test passes.
6464
<li>Could you solve this problem in constant space complexity with a linear time algorithm?</li>
6565
</ul>
6666

67-
6867
## Solutions
6968

7069
<!-- tabs:start -->
7170

7271
### **Python3**
7372

7473
```python
75-
74+
"""
75+
# Definition for a Node.
76+
class Node:
77+
def __init__(self, val=None, children=None):
78+
self.val = val
79+
self.children = children if children is not None else []
80+
"""
81+
82+
class Solution:
83+
def findRoot(self, tree: List['Node']) -> 'Node':
84+
xorsum = 0
85+
for node in tree:
86+
xorsum ^= node.val
87+
for child in node.children:
88+
xorsum ^= child.val
89+
90+
for node in tree:
91+
if node.val == xorsum:
92+
return node
7693
```
7794

7895
### **Java**
7996

8097
```java
98+
/*
99+
// Definition for a Node.
100+
class Node {
101+
public int val;
102+
public List<Node> children;
103+
104+
105+
public Node() {
106+
children = new ArrayList<Node>();
107+
}
108+
109+
public Node(int _val) {
110+
val = _val;
111+
children = new ArrayList<Node>();
112+
}
113+
114+
public Node(int _val,ArrayList<Node> _children) {
115+
val = _val;
116+
children = _children;
117+
}
118+
};
119+
*/
120+
121+
class Solution {
122+
public Node findRoot(List<Node> tree) {
123+
int xor = 0;
124+
for (Node node : tree) {
125+
xor ^= node.val;
126+
for (Node child : node.children) {
127+
xor ^= child.val;
128+
}
129+
}
130+
for (Node node :tree) {
131+
if (node.val == xor) {
132+
return node;
133+
}
134+
}
135+
return null;
136+
}
137+
}
138+
```
139+
140+
### **C++**
141+
142+
```cpp
143+
/*
144+
// Definition for a Node.
145+
class Node {
146+
public:
147+
int val;
148+
vector<Node*> children;
149+
150+
Node() {}
151+
152+
Node(int _val) {
153+
val = _val;
154+
}
155+
156+
Node(int _val, vector<Node*> _children) {
157+
val = _val;
158+
children = _children;
159+
}
160+
};
161+
*/
162+
163+
class Solution {
164+
public:
165+
Node* findRoot(vector<Node*> tree) {
166+
int xorsum = 0;
167+
for (auto& node : tree) {
168+
xorsum ^= node->val;
169+
for (auto& child : node->children) {
170+
xorsum ^= child->val;
171+
}
172+
}
173+
for (auto& node : tree) {
174+
if (node->val == xorsum) {
175+
return node;
176+
}
177+
}
178+
return nullptr;
179+
}
180+
};
181+
```
81182
183+
### **Go**
184+
185+
```go
186+
/**
187+
* Definition for a Node.
188+
* type Node struct {
189+
* Val int
190+
* Children []*Node
191+
* }
192+
*/
193+
194+
func findRoot(tree []*Node) *Node {
195+
xorsum := 0
196+
for _, node := range tree {
197+
xorsum ^= node.Val
198+
for _, child := range node.Children {
199+
xorsum ^= child.Val
200+
}
201+
}
202+
for _, node := range tree {
203+
if node.Val == xorsum {
204+
return node
205+
}
206+
}
207+
return nil
208+
}
82209
```
83210

84211
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
// Definition for a Node.
3+
class Node {
4+
public:
5+
int val;
6+
vector<Node*> children;
7+
8+
Node() {}
9+
10+
Node(int _val) {
11+
val = _val;
12+
}
13+
14+
Node(int _val, vector<Node*> _children) {
15+
val = _val;
16+
children = _children;
17+
}
18+
};
19+
*/
20+
21+
class Solution {
22+
public:
23+
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;
29+
}
30+
}
31+
for (auto& node : tree) {
32+
if (node->val == xorsum) {
33+
return node;
34+
}
35+
}
36+
return nullptr;
37+
}
38+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/**
2+
* Definition for a Node.
3+
* type Node struct {
4+
* Val int
5+
* Children []*Node
6+
* }
7+
*/
8+
9+
func findRoot(tree []*Node) *Node {
10+
xorsum := 0
11+
for _, node := range tree {
12+
xorsum ^= node.Val
13+
for _, child := range node.Children {
14+
xorsum ^= child.Val
15+
}
16+
}
17+
for _, node := range tree {
18+
if node.Val == xorsum {
19+
return node
20+
}
21+
}
22+
return nil
23+
}

0 commit comments

Comments
 (0)