# 利用栈进行二叉树前序遍历

利用栈进行二叉树前序遍历[leetcode题号144]

请准确描述实现代码时间复杂度和空间复杂度~

https://leetcode.cn/problems/binary-tree-preorder-traversal/

var preorderTraversal = function (root) {
    const arr = [];

    if (root !== null) {
        traversal(root, arr)
    }
    return arr
};

const traversal = function (root, arr) {
    arr.push(root.val)
    if (root.left) {
        traversal(root.left, arr)
    }
    if (root.right) {
        traversal(root.right, arr)
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

时间复杂度On 空间复杂度 On