Skip to content

Commit 1ad5623

Browse files
authored
Create 589-n-ary-tree-preorder-traversal.js
1 parent 7855d50 commit 1ad5623

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

589-n-ary-tree-preorder-traversal.js

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

0 commit comments

Comments
 (0)