Skip to content

Commit 5b157f4

Browse files
authored
Update 1506-find-root-of-n-ary-tree.js
1 parent a5b2cee commit 5b157f4

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

1506-find-root-of-n-ary-tree.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,31 @@ const findRoot = function(tree) {
2323
}
2424
return null
2525
};
26+
27+
// another
28+
29+
/**
30+
* // Definition for a Node.
31+
* function Node(val, children) {
32+
* this.val = val === undefined ? 0 : val;
33+
* this.children = children === undefined ? [] : children;
34+
* };
35+
*/
36+
37+
/**
38+
* @param {Node[]} tree
39+
* @return {Node}
40+
*/
41+
const findRoot = function(tree) {
42+
let sum = 0
43+
for(let n of tree) {
44+
sum ^= n.val
45+
for(let c of n.children) {
46+
sum ^= c.val
47+
}
48+
}
49+
for(let n of tree) {
50+
if(n.val === sum) return n
51+
}
52+
return null
53+
};

0 commit comments

Comments
 (0)