Sunny-117/js-challenges

二叉树的前序遍历

Sunny-117 opened this issue · 1 comments

二叉树的前序遍历
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var preorderTraversal = function(root) {
    if(!root) return [];
    let res = [];
    const dfs = (node) => {
        res.push(node.val);
        node.left && dfs(node.left)
        node.right && dfs(node.right)
    }
    dfs(root);
    return res;
};