代码随想录算法训练营第16天 | 101、104、111、222

101.对称二叉树

题目描述

给你一个二叉树的根节点 root , 检查它是否轴对称。
示例1:
输入:root=[1,2,2,3,4,4,3]root = [1,2,2,3,4,4,3]root=[1,2,2,3,4,4,3]
输出:truetruetrue
示例2:
输入:root=[1,2,2,null,3,null,3]root = [1,2,2,null,3,null,3]root=[1,2,2,null,3,null,3]
输出:falsefalsefalse

思路

本题的思路很明确,看到时第一时间想到的就是递归法,可以对比根节点的左右子树,看是否对称,还是很容易想到的。
非递归的方法也是相同的套路,可以左前序遍历,右子树只需要改变左右的遍历顺序,即可达到相同的效果。

解法1

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        return helper(root.left,root.right);
    }
    public boolean helper(TreeNode root1,TreeNode root2){
        if(root1 == null && root2 == null) return true;
        if(root1 == null || root2 == null) return false;
        return root1.val == root2.val && helper(root1.left,root2.right) && helper(root1.right,root2.left);
    }
}

解法2

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isSymmetric(TreeNode root) {
        if(root == null) return true;
        Stack<TreeNode> stk = new Stack<>();
        stk.push(root.left);
        stk.push(root.right);
        while(!stk.isEmpty()){
            TreeNode leftNode = stk.pop();
            TreeNode rightNode = stk.pop();
            if(leftNode == null && rightNode == null) continue;
            if(leftNode == null || rightNode == null || leftNode.val != rightNode.val){
                return false;
            }
            stk.push(leftNode.left);
            stk.push(rightNode.right);
            stk.push(leftNode.right);
            stk.push(rightNode.left);
        }
        return true;
    }
}

总结

简单题,按顺序比较即可。

104. 二叉树的最大深度

题目描述

给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
示例:
输入:[3,9,20,null,null,15,7][3,9,20,null,null,15,7][3,9,20,null,null,15,7]
输出:333

思路

最大深度其实就是树的高度,可以很容易想到递归法,一层一层递归,一层一加一。迭代法也是类似,层序遍历在输出的位置给深度加1,最终输出。

解法1

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        return Math.max(leftDepth,rightDepth)+1;
    }
}

解法2

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;
        Queue<TreeNode> que = new LinkedList<>();
        TreeNode cur = root;
        que.offer(cur);
        int count = 0;
        while(!que.isEmpty()){
            int size = que.size();
            count++;
            for(int i=0;i<size;i++){
                cur = que.poll();
                if(cur.left != null) que.offer(cur.left);
                if(cur.right!=null) que.offer(cur.right);
            }
        }
        return count;
    }
}

总结

简单题,层序遍历的迭代法要更熟练一些。

111. 二叉树的最小深度

题目描述

给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
示例1:
输入:root=[3,9,20,null,null,15,7]root = [3,9,20,null,null,15,7]root=[3,9,20,null,null,15,7]
输出:222
示例2:
输入:root=[2,null,3,null,4,null,5,null,6]root = [2,null,3,null,4,null,5,null,6]root=[2,null,3,null,4,null,5,null,6]
输出:555

思路

本题和最大深度的思路一致,只不过是输出最小的。

解法1

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        int leftDepth = minDepth(root.left);
        int rightDepth = minDepth(root.right);
        if(leftDepth == 0) return rightDepth+1;
        if(rightDepth == 0) return leftDepth+1;
        return Math.min(leftDepth,rightDepth)+1;
    }
}

解法2

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        Queue<TreeNode> que = new LinkedList<>();
        int count = 0;
        TreeNode cur = root;
        que.offer(cur);
        while(!que.isEmpty()){
            int size = que.size();
            count++;
            for(int i=0;i<size;i++){
                cur = que.poll();
                if(cur.left!=null) que.offer(cur.left);
                if(cur.right!=null) que.offer(cur.right);
                if(cur.left == null && cur.right == null) return count;
            }
        }
        return count;
    }
}

总结

能看出来和最大深度差不多然后能想到这个思路就行。写出来的时候要注意判断条件,只有左右孩子都为空的情况才为叶子。

222.完全二叉树的节点个数

题目描述

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h2^h2h 个节点。
示例1:
输入:root=[1,2,3,4,5,6]root = [1,2,3,4,5,6]root=[1,2,3,4,5,6]
输出:666
示例2:
输入:root=[]root = []root=[]
输出:000
示例3:
输入:root=[1]root = [1]root=[1]
输出:111

思路

如果仅仅是一棵二叉树,那么遍历即可,没什么可想的,而且也能通过。但是是个完全二叉树,就想要利用这个条件。想要利用完全二叉树的性质根据高度求节点数量,那其实就又变成了类似的求深度的问题。

解法

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int countNodes(TreeNode root) {
        if(root == null) return 0;
        TreeNode leftNode = root.left;
        TreeNode rightNode = root.right;
        int leftDepth = 0,rightDepth = 0;
        while(leftNode != null){
            leftNode = leftNode.left;
            leftDepth++;
        }
        while(rightNode != null){
            rightNode = rightNode.right;
            rightDepth++;
        }
        if(leftDepth == rightDepth){
            return (2<<leftDepth)-1;
        }
        return countNodes(root.left)+countNodes(root.right)+1;

    }
}

总结

初初看到时确实还是要想想怎么利用完全二叉树的条件的,要能够想到转化为高度的问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值