We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent a5b2cee commit 5b157f4Copy full SHA for 5b157f4
1506-find-root-of-n-ary-tree.js
@@ -23,3 +23,31 @@ const findRoot = function(tree) {
23
}
24
return null
25
};
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
50
+ if(n.val === sum) return n
51
52
+ return null
53
+};
0 commit comments