Skip to content

Commit c48ab64

Browse files
committed
Time: 63 ms (10.59%), Space: 49.4 MB (7.64%) - LeetHub
1 parent 860f6ad commit c48ab64

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[]}
12+
*/
13+
var preorderTraversal = function(root) {
14+
const result = [];
15+
function preorder(node) {
16+
if (node) {
17+
result.push(node.val);
18+
preorder(node.left);
19+
preorder(node.right);
20+
}
21+
}
22+
preorder(root);
23+
return result;
24+
};

0 commit comments

Comments
 (0)