代码随想录算法训练营第十四天 | 226.翻转二叉树、 101. 对称二叉树、 104.二叉树的最大深度、 111.二叉树的最小深度

LeetCode 226.翻转二叉树

题目链接

视频地址

第一想法:迭代+左右节点交换,返回root

实现过程:

  var invertTree = function (root) {
    
    invert(root)
   
    return root

  }
  var invert = function (cur) {
      if (!cur) return
      [cur.left, cur.right] = [cur.right, cur.left]
      invert(cur.left)
      invert(cur.right)
      return cur
 }

学后总结:这里递归可以用前序遍历或者后序遍历,如果用中序遍历会有右子树不被遍历的情况下产生。

LeetCode 101. 对称二叉树

题目链接

视频地址

第一想法:递归+对比左右节点,这里要注意对比左右节点的条件,会有节点为null的情况

实现过程:

 var isSymmetric = function(root) {
    return compare(root.left, root.right)
  }
  var compare = function (left, right) { 
    if (!left && !right) return true
    if (!left || !right) return false
    if (left.val !== right.val) return false
    return compare(left.left, right.right) && compare(left.right, right.left)
  }

学后总结:这道题只能用后序遍历,因为左右孩子的对比结果要集合返回给它们的父节点。

LeetCode 104.二叉树的最大深度

题目链接

视频地址

第一想法:通过层序遍历有几层最大深度就是几

实现过程:

var maxDepth = function(root) {
    if(!root) return 0
    let maxCount = 0
    let temp = [root]
    while (temp.length) {
      let count = temp.length
      maxCount++
      while (count) {
        let item = temp.shift()
        if (item.left) {
          temp.push(item.left)
        }
        if (item.right) {
          temp.push(item.right)
        }
        count--
      }
    }
    return maxCount
};

学后总结:

  • 深度是节点到根节点的距离,高度是二叉树中任意一个节点到叶子节点的距离。求高度用后序遍历,求深度用前序遍历。
  • 这道题的解法是求根节点的高度,也就是这棵树的最大深度。

var maxdepth = function(root) {
    if (root === null) return 0;
    return 1 + Math.max(maxdepth(root.left), maxdepth(root.right))
};

// 递归
var maxdepth = function(root) {
    //使用递归的方法 递归三部曲
    //1. 确定递归函数的参数和返回值
    const getdepth = function(node) {
    //2. 确定终止条件
        if(node === null) {
            return 0;
        }
    //3. 确定单层逻辑
        let leftdepth = getdepth(node.left);
        let rightdepth = getdepth(node.right);
        let depth = 1 + Math.max(leftdepth, rightdepth);
        return depth;
    }
    return getdepth(root);
};

LeetCode 111.二叉树的最小深度

题目链接

视频地址

第一想法:通过层序遍历,当第一次找到节点没有左右孩子的时候就代表最小的层级,返回即可。

实现过程:

var minDepth = function(root) {
    if(!root) return 0
    let res = 0
    let temp = [root]
    while (temp.length) {
      let count = temp.length
      res++
      while (count) {
        let item = temp.shift()

        if (!item.left && !item.right) {
          return res
        }
        if (item.left) {
          temp.push(item.left)
        }
        if (item.right) {
          temp.push(item.right)
        }
        count--
      }
    }
    console.log('res', res)
	return res

};

学后总结:

var minDepth1 = function(root) {
    if(!root) return 0;
    // 到叶子节点 返回 1
    if(!root.left && !root.right) return 1;
    // 只有右节点时 递归右节点
    if(!root.left) return 1 + minDepth(root.right);
    // 只有左节点时 递归左节点
    if(!root.right) return 1 + minDepth(root.left);
    return Math.min(minDepth(root.left), minDepth(root.right)) + 1;
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值