leetcode 144. 二叉树的前序遍历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34629352/article/details/85047945
/**
 * 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)

};

猜你喜欢

转载自blog.csdn.net/qq_34629352/article/details/85047945