Skip to content

Commit ef39957

Browse files
authored
Create 1490-clone-n-ary-tree.js
1 parent 2e9e92e commit ef39957

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

1490-clone-n-ary-tree.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* // Definition for a Node.
3+
* function Node(val, children) {
4+
* this.val = val === undefined ? 0 : val;
5+
* this.children = children === undefined ? [] : children;
6+
* };
7+
*/
8+
9+
/**
10+
* @param {Node} node
11+
* @return {Node}
12+
*/
13+
const cloneTree = function(root) {
14+
if(root == null) return null
15+
let node = new Node(root.val)
16+
for(let i = 0, len = root.children.length; i < len; i++) {
17+
node.children.push(cloneTree(root.children[i]))
18+
}
19+
return node
20+
};

0 commit comments

Comments
 (0)