刷题98—树(五)

题152-题154

152.对称的二叉树

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目描述

请实现一个函数,用来判断一棵二叉树是不是对称的。如果一棵二叉树和它的镜像一样,那么它是对称的。

例如,二叉树 [1,2,2,3,4,4,3] 是对称的。

    1
   /
  2   2
 / /
3  4 4  3
但是下面这个 [1,2,2,null,3,null,3] 则不是镜像对称的:

    1
   /
  2   2
     
   3    3

示例 1:

输入:root = [1,2,2,3,4,4,3]
输出:true
示例 2:

输入:root = [1,2,2,null,3,null,3]
输出:false
 

限制:

0 <= 节点个数 <= 1000

注意:本题与主站 101 题相同:https://leetcode-cn.com/problems/symmetric-tree/

题目分析

  1. r1,r2分别是根节点的左右子树;
  2. 若是r1,r2都为空,返回true;
  3. 若是r1,r2有一个为空,返回false;
  4. 判断r1的左子树和r2的右子树、r1的右子树和r2的左子树是否相等,并返回。
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isSymmetric = function(root) {
    function isMirror(r1,r2){
        if(!r1 && !r2) return true;
        if(!r1 || !r2) return false;
        return r1.val == r2.val && isMirror(r1.left,r2.right) && isMirror(r1.right,r2.left);
    }
    return isMirror(root,root);
};

  

153.平衡二叉树

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/ping-heng-er-cha-shu-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目描述

输入一棵二叉树的根节点,判断该树是不是平衡二叉树。如果某二叉树中任意节点的左右子树的深度相差不超过1,那么它就是一棵平衡二叉树。

示例 1:

给定二叉树 [3,9,20,null,null,15,7]

3
/
9 20
/
15 7
返回 true 。

示例 2:

给定二叉树 [1,2,2,3,3,null,null,4,4]

1
/
2 2
/
3 3
/
4 4
返回 false 。

限制:

1 <= 树的结点个数 <= 10000
注意:本题与主站 110 题相同:https://leetcode-cn.com/problems/balanced-binary-tree/

题目分析

  1. 设置flag = true,flag用来判断是不是平衡二叉树;
  2. 分别遍历左子树和右子树;
  3. 计算每次遍历后的左子树和右子树高度差,高度差大于1的时候,flag=false;
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {boolean}
 */
var isBalanced = function(root) {
    let flag = true;
    judge(root);
    return flag;
    function judge(root){
        if(!root) return null;
        let left = judge(root.left);
        let right = judge(root.right);
        if(Math.abs(left-right) > 1) flag = false;
        return left > right ? left+1:right+1;
    }

};

  

154.二叉树的深度

题目链接

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题目描述

输入一棵二叉树的根节点,求该树的深度。从根节点到叶节点依次经过的节点(含根、叶节点)形成树的一条路径,最长路径的长度为树的深度。

例如:

给定二叉树 [3,9,20,null,null,15,7],

3
/
9 20
/
15 7
返回它的最大深度 3 。

提示:

节点总数 <= 10000
注意:本题与主站 104 题相同:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

题目分析

  1. 遍历二叉树的左子树和右子树,直到为空;
  2. 判断找出当前最长路径,并记录。
/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number}
 */
var maxDepth = function(root) {
    if(!root) return 0;
    let max = 0;
    rode(root,0);
    function rode(root,num){
        if(!root.left && !root.right){
            if(max < num+1) max = num+1;
        }
        if(root.left) rode(root.left,num+1);
        if(root.right) rode(root.right,num+1);
    }
    return max;
};

  

原文地址:https://www.cnblogs.com/liu-xin1995/p/12880005.html