代码随想录四刷day16

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


本周的主题其实是简单但并不简单,本周所选的题目大多是看一下就会的题目,但是大家看完本周的文章估计也发现了,二叉树的简单题目其实里面都藏了很多细节。 这些细节我都给大家展现了出来。

一、力扣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 {
    Deque<TreeNode> deq = new LinkedList<>();
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        deq.offerLast(root);
        int res = 0;
        while(!deq.isEmpty()){
            int size = deq.size();
            res ++;
            for(int i = 0; i <size; i ++){
                TreeNode cur = deq.pollFirst();
                if(cur.left == null && cur.right == null){
                    return res;
                }
                if(cur.left != null){
                    deq.offerLast(cur.left);
                }
                if(cur.right != null){
                    deq.offerLast(cur.right);
                }
            }
        }
        return res;
    }
}

递归后序

/**
 * 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;
        }
        return fun(root,0);
    }
    public int fun(TreeNode root, int depth){
        if(root == null){
            return Integer.MAX_VALUE;
        }
        if(root.left == null && root.right == null){
            return depth+1;
        }
        int l = fun(root.left, depth+1);
        int r = fun(root.right, depth+1);
        return Math.min(l,r);
    }
}

前序递归

/**
 * 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 {
    int res = Integer.MAX_VALUE, len = 0;
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        preOrder(root);
        return res;
    }
    public void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        len ++;
        if(root.left == null && root.right == null){
            res = Math.min(res,len);
        }
        preOrder(root.left);
        preOrder(root.right);
        len --;
    }
}

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

后序

/**
 * 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) {
        return fun(root);
    }
    public int fun(TreeNode root){
        if(root == null){
            return 0;
        }
        int l = fun(root.left);
        int r = fun(root.right);
        return l + r + 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 {
    int res = 0;
    public int countNodes(TreeNode root) {
        fun(root);
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        res ++;
        fun(root.left);
        fun(root.right);
    }
}

三、力扣110. 平衡二叉树

/**
 * 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 {
    boolean flag = true;
    public boolean isBalanced(TreeNode root) {
        fun(root);
        return flag;
    }
    public int fun(TreeNode root){
        if(root == null){
            return 0;
        }
        int l = fun(root.left);
        int r = fun(root.right);
        if(Math.abs(l-r) > 1){
            flag = false;
        }
        return l > r ? l + 1 : r + 1;
    }
}

四、力扣257. 二叉树的所有路径

/**
 * 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 {
    List<String> res = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        if(root == null){
            return res;
        }
        fun(root);
        return res;
    }
    public void fun(TreeNode root){
        if(root == null){
            return;
        }
        list.add(root.val);
        if(root.left == null && root.right == null){
            String s = "";
            for(int i = 0; i < list.size(); i ++){
                s += list.get(i);
                if(i < list.size()-1){
                    s += "->";
                }
            }
            res.add(s);
        }
        fun(root.left);
        fun(root.right);
        list.remove(list.size()-1);
    }
}
  • 5
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

乱世在摸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值