1. 程式人生 > >leetcode 144. 二叉樹的前序遍歷

leetcode 144. 二叉樹的前序遍歷

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var preorderTraversal = function(root) {
    var result  = []
    var traversal = function(root) {
      if (root) {
        // 先序
        console.log(result.push(root.val)); 
        traversal(root.left);
        // 中序
        // console.log(root); 
        traversal(root.right);
        // 後序
        // console.log(root);
      }
        return result
    };
  return traversal(root)

};