代码随想录——第十五天

第六章 二叉树part03

迭代法,大家可以直接过,二刷有精力的时候 再去掌握迭代法。

110.平衡二叉树 (优先掌握递归)

再一次涉及到,什么是高度,什么是深度,可以巩固一下。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0110.%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91.html 

class Solution {

    public boolean isBalanced(TreeNode root) {

        if(root == null) return true;

        boolean left = isBalanced(root.left);

        boolean right = isBalanced(root.right);

        boolean mid = Math.abs(deep(root.left)-deep(root.right)) < 2;

        return left && right && mid;

    }

    private int deep (TreeNode root){

        if(root == null){

            return 0;

        }

        return 1 + Math.max(deep(root.left), deep(root.right));

    }

}

257. 二叉树的所有路径 (优先掌握递归)

这是大家第一次接触到回溯的过程, 我在视频里重点讲解了 本题为什么要有回溯,已经回溯的过程。

如果对回溯 似懂非懂,没关系, 可以先有个印象。

题目链接/文章讲解/视频讲解:https://programmercarl.com/0257.%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E6%89%80%E6%9C%89%E8%B7%AF%E5%BE%84.html

错误写法:

class Solution {

    public List<String> binaryTreePaths(TreeNode root) {

        List<String> res = new ArrayList<>();

        List<TreeNode> path = new LinkedList<>();

        if(root.left == null && root.right == null){

            StringBuilder sb = "";

            for(int i = 0; i < path.size(); i ++){

                if(i != path.size()-1){

                    sb += path[i].val;

                    sb += "->";

                }else{

                    sb += path[i].val;

                }

            }

            String s = sb.toString();

            res.add(s);

        }

        if(root.left != null){

            List<String> left = binaryTreePaths(root.left);

            path.add(root.left);

            path.remove(root.left);

        }

        if(root.right != null){

            List<String> right = binaryTreePaths(root.right);

            path.add(root.right);

            path.remove(root.right);

        }

    }

}

明天补上;

404.左叶子之和 (优先掌握递归)

其实本题有点文字游戏,搞清楚什么是左叶子,剩下的就是二叉树的基本操作。

题目链接/文章讲解/视频讲解:

class Solution {

    public int sumOfLeftLeaves(TreeNode root) {

        if(root == null){return 0;}

        int left = sumOfLeftLeaves(root.left);

        int right = sumOfLeftLeaves(root.right);

        int val = 0;

        if(root.left != null && root.left.left == null && root.left.right == null){

            val = root.left.val;

        }

        return val + left + right;

    }

}

222.完全二叉树的节点个数(优先掌握递归)

需要了解,普通二叉树 怎么求,完全二叉树又怎么求

题目链接/文章讲解/视频讲解:https://programmercarl.com/0222.%E5%AE%8C%E5%85%A8%E4%BA%8C%E5%8F%89%E6%A0%91%E7%9A%84%E8%8A%82%E7%82%B9%E4%B8%AA%E6%95%B0.html 

class Solution {

    public int countNodes(TreeNode root) {

        if (root == null) return 0;

        int left = countNodes(root.left);

        int right = countNodes(root.right);

        return 1 + left + right;

    }

}



class Solution {

    /**

     * 针对完全二叉树的解法

     *

     * 满二叉树的结点数为:2^depth - 1

     */

    public int countNodes(TreeNode root) {

        if (root == null) return 0;

        TreeNode left = root.left;

        TreeNode right = root.right;

        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便

        while (left != null) {  // 求左子树深度

            left = left.left;

            leftDepth++;

        }

        while (right != null) { // 求右子树深度

            right = right.right;

            rightDepth++;

        }

        if (leftDepth == rightDepth) {

            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0

        }

        return countNodes(root.left) + countNodes(root.right) + 1;

    }

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。