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 2ae66f9 commit c6de2b4Copy full SHA for c6de2b4
algorithms/n-ary-tree-preorder-traversal.js
@@ -26,4 +26,27 @@ var preorder = function (root) {
26
dfs(root);
27
28
return result;
29
+
30
+ // BFS
31
+ // return bfs(root);
32
};
33
34
+// BFS
35
+function bfs(root) {
36
+ const result = [];
37
+ const stack = [];
38
+ stack.push(root);
39
40
+ if (!root) return [];
41
42
+ while (stack.length) {
43
+ const node = stack.pop();
44
+ const children = node.children;
45
+ result.push(node.val);
46
+ for (let i = children.length - 1; i >= 0; i--) {
47
+ stack.push(children[i]);
48
+ }
49
50
51
+ return result;
52
+}
0 commit comments