二叉树的前序遍历
Sunny-117 opened this issue · 1 comments
Sunny-117 commented
二叉树的前序遍历
lxy-Jason commented
/**
* @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;
};