递归前序遍历二叉树
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number[]}
*/
var preorderTraversal = function(root) {
let array = [];
digui(root);
function digui(root) {
if (root == null) return;
// 根节点, 左子树, 右子树
array.push(root.val);
digui(root.left);
digui(root.right);
}
return array;
};