二叉树专题

114 阅读1分钟

1.层序遍历

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var levelOrder = function(root) {
    if(!root) {
        return []
    }
    var list = [root]
    var res = [];
    while(list.length) {
        var obj = list.shift();
        res.push(obj.val);
        if(obj.left){
            list.push(obj.left);
        }
        if(obj.right){
            list.push(obj.right)
        }
    }
    return res;
};

2.从上到下打印二叉树 II

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[][]}
 */
var levelOrder = function(root) {
  if(!root) {
      return []
  }
  var list = [root];
  var res = [];
  while(list.length) {
      var leveList = [];
      var levelNum = list.length
      while(levelNum--){
          var obj = list.shift();
          leveList.push(obj.val);
          if(obj.left) {
              list.push(obj.left)
          }
          if(obj.right) {
              list.push(obj.right)
          }
      }
      res.push(leveList);
  }
  return res;
};

3.翻转二叉树

/**
 * 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 {TreeNode}
 */
var invertTree = function(root) {
    if(!root){
        return root;
    }
    var temp = root.right;
    root.right = root.left;
    root.left = temp;
    invertTree(root.left);
    invertTree(root.right);
    return root;
       
};

4.二叉树的深度

/**
 * 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 maxDepth = function(root) {
    if(root==null){
        return 0;
    } else {
        var left = maxDepth(root.left);
        var right = maxDepth(root.right);
        return Math.max(left,right)+1
    }

};

6.二叉树的前序遍历

/**
 * 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) {
    var list = []
    if(!root){
        return list;
    }
   var dfs = function(root){
        if (!root){
               return null;
            }
            list.push(root.val)
            if(root.left){
               test(root.left);
            }
            if(root.right){
                test(root.right);
            } 
        }
    dfs(root);
    return list;
};