代码随想录算法训练营第十六天 | 104. 二叉树的最大深度 、559. N 叉树的最大深度、111. 二叉树的最小深度、222. 完全二叉树的节点个数、222. 完全二叉树的节点个数

[LeetCode] 104. 二叉树的最大深度

[LeetCode] 104. 二叉树的最大深度 文章说明

[LeetCode] 104. 二叉树的最大深度 视频说明

给定一个二叉树 root ,返回其最大深度。

二叉树的 最大深度 是指从根节点到最远叶子节点的最长路径上的节点数。

示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:3

示例 2:

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

提示:

  • 树中节点的数量在 [0, 104] 区间内。
  • -100 <= Node.val <= 100

自己看到题目的第一想法

    昨天已经刷过了, 应该比较简单.

看完代码随想录之后的想法

    树的高度和深度是不一样的, 但是我们可以通过求高度得到树的深度, 反过来亦可.

    这一题可以用前序、后续、迭代法来求.

/**
 * 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;
 *     }
 * }
 */
// 递归法: 后续遍历, 算出高度后, 反推深度. 因为高度和深度的值是一样的.
// 判断是高度还是深度, 就看叶子结点的值, 如果叶子结点的值为1, 则计算的是高度.
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1;
    }
}
//递归法: 先序遍历(这里算是先序遍历吗?), 算出深度后, 反推高度. 因为高度和深度的值是一样的.
//判断是高度还是深度, 就看叶子结点的值, 如果叶子结点的值为1, 则计算的是高度.
class Solution {
    public int maxDepth(TreeNode root) {
        return maxDepth(root, 1);
    }

    private int maxDepth(TreeNode root, int depth) {
        if (root == null) {
            return depth - 1;
        }
        return Math.max(
                maxDepth(root.left, depth + 1), 
                maxDepth(root.right, depth + 1));
    }
}
// 迭代法: 效率低一些
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        int depth = 0;
        LinkedList<TreeNode> nodes = new LinkedList<>();
        TreeNode node = null;
        nodes.addLast(root);
        while (!nodes.isEmpty()) {
            depth++;
            int size = nodes.size();
            while (size-- > 0) {
                node = nodes.pop();
                if (node.right != null) {
                    nodes.addLast(node.right);
                }
                if (node.left != null) {
                    nodes.addLast(node.left);
                }
            }
        }
        return depth;
    }
}

自己实现过程中遇到哪些困难

    因为昨天自己实现了一遍, 今天又看了视频讲解, 因此今天编码的时候没有遇到太大的困难. 二叉树的递归遍历, 普通迭代法, 统一迭代法都比较熟悉了. 就是不知道时间长了之后, 还能不能记得住.

[LeetCode] 559. N 叉树的最大深度

[LeetCode] 559. N 叉树的最大深度 文章解释

[LeetCode] 559. N 叉树的最大深度 视频解释


 

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。

示例 1:

输入:root = [1,null,3,2,4,null,5,6]
输出:3

示例 2:

输入:root = [1,null,2,3,4,5,null,null,6,7,null,8,null,9,10,null,null,11,null,12,null,13,null,null,14]
输出:5

提示:

  • 树的深度不会超过 1000
  • 树的节点数目位于 [0, 10^4] 之间。

自己看到题目的第一想法

    1. 迭代法: 从上往下层序遍历每一层节点, 遍历一层深度加一, 直到最后一层遍历完成.

    2. 递归法: 通过计算当前节点的左节点的高度和右节点的高度, 取两者中高度更大的那一个, 加上一, 就是当前节点的高度.

看完代码随想录之后的想法

    这两天感觉有点混乱, 可能是一下子刷太多题了, 脑子也有点懵.

    但是有一点我记得很清楚, 就是在做递归的时候, 自己心里要清楚目前是前序遍历还是后序遍历. 这样是清楚的掌握了代码的执行流程.

    通过 debug 或者画图的方式, 把代码修修补补到 AC, 往往脑子里还是不清晰的. 刷算法最终还是要心静下来. 但是确实不知道到底还有多少未知的领域等待自己摸索, 动态规划会很难吗?

    无论如何, 加油!

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val) {
        val = _val;
    }

    public Node(int _val, List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
// 前序遍历
class Solution {
    public int maxDepth(Node root) {
        if(root == null) {
            return 0;
        }
        int depth = 1;
        for (int i = 0; root.children != null && i < root.children.size(); i++) {
            depth = Math.max(maxDepth(root.children.get(i)) + 1, depth);
        }
        return depth;
    }
}
// 后序遍历
class Solution {
    public int maxDepth(Node root) {
        return maxDepth(root, 1);
    }
    private int maxDepth(Node node, int depth) {
        if (node == null) {
            return depth - 1;
        }

        int maxDepth = depth;        
        for (int i = 0; node.children != null && i < node.children.size(); i++) {
            maxDepth = Math.max(maxDepth(node.children.get(i), depth + 1), maxDepth);
        }
        return maxDepth;
    }
}
// 迭代法: 效率低
class Solution {
    public int maxDepth(Node root) {
        if(root == null) {
            return 0;
        }
        int depth = 0;
        LinkedList<Node> nodes = new LinkedList<>();
        Node node = null;
        nodes.addLast(root);
        while (!nodes.isEmpty()) {
            depth++;
            int size = nodes.size();
            while (size-- > 0) {
                node = nodes.pop();
                for (int i = 0; node.children != null && i < node.children.size(); i++) {
                    nodes.addLast(node.children.get(i));
                }
            }
        }
        
        return depth;
    }
}

自己实现过程中遇到哪些困难 

    今天的实现过程异常的轻松, 因为遍历二叉树的递归法, 迭代法, 统一风格法自己都很清楚了. 希望过一段时间可别忘记了.

[LeetCode] 111. 二叉树的最小深度

[LeetCode] 111. 二叉树的最小深度 文章解释

[LeetCode] 111. 二叉树的最小深度 视频解释

给定一个二叉树,找出其最小深度。

最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

说明:叶子节点是指没有子节点的节点。

示例 1:

输入:root = [3,9,20,null,null,15,7]
输出:2

示例 2:

输入:root = [2,null,3,null,4,null,5,null,6]
输出:5

提示:

  • 树中节点数的范围在 [0, 10^5]
  • -1000 <= Node.val <= 1000

自己看到题目的第一想法

    1. 递归法: 计算左子树的高度, 再计算右子树的高度, 比较两者的最小值, 将最小值加上1就是当前节点的最小高度.

    2. 迭代法: 从上往下层序遍历节点, 遇到第一个没有子节点的节点, 该节点的深度就是当前树的最小深度.

看完代码随想录之后的想法

    视频里主要介绍了递归法的实现方式. 这里我一开始确实也犯了同样的错误, 比如当跟节点的左子树为空时, 我会返回最小深度为1. 然而这时候其实应该返回右子树的最小深度.

    这里通过左右子节点是否为空, 来决定最小深度的获取方式, 让代码变得更精简了一些. 最开始我是通过 leftTreeSize 和 rightTreeSize 是否为 0 来决定是否舍弃其中的某个数据, 但是感觉没有通过左右节点是否非空来判断更易读.

/**
 * 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 childDepth = 0;
        if (root.left != null && root.right == null) {
            childDepth = minDepth(root.left);
        } else if (root.left == null && root.right != null){
            childDepth = minDepth(root.right);
        } else if (root.left == null && root.right == null) {
            childDepth = 0;
        } else {
            childDepth = Math.min(minDepth(root.left), minDepth(root.right));
        }
        return childDepth + 1;
    }
}
// 后序遍历
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return minDepth(root, 1);
    }

    private int minDepth(TreeNode root, int depth) {
        if (root == null) {
            return depth - 1;
        }
        int childDepth = 0;
        if (root.left != null && root.right == null) {
            childDepth = minDepth(root.left, depth + 1);
        } else if (root.left == null && root.right != null){
            childDepth = minDepth(root.right, depth + 1);
        } else if (root.left == null && root.right == null) {
            childDepth = depth;
        } else {
            childDepth = Math.min(minDepth(root.left, depth + 1), minDepth(root.right, depth + 1));
        }
        return childDepth;
    }
}
// 迭代法: 高效. 
// 因为遇到一个叶子结点就可以直接结束, 因此不需要遍历全部的节点. 
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        LinkedList<TreeNode> nodes = new LinkedList<>();
        TreeNode node;
        int size = 0;
        int depth = 0;
        
        nodes.addLast(root);
        while (!nodes.isEmpty()) {
            depth++;
            size = nodes.size();
            while (size-- > 0) {
                node = nodes.pop();
                if (node.left == null && node.right == null) {
                    return depth;
                }
                if (node.right != null) {
                    nodes.addLast(node.right);
                }
                if (node.left != null) {
                    nodes.addLast(node.left);
                }
            }
        }
        return depth;
    }
}

自己实现过程中遇到哪些困难 

    当左节点或者右节点有一个为空时, 最小高度会计算错误. 会认为空节点的最小高度为0, 因此当前节点的最小高度为1. 这是错误的.

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

[LeetCode] 222. 完全二叉树的节点个数 文章解释

[LeetCode] 222. 完全二叉树的节点个数 视频解释
 

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

示例 1:

输入:root = [1,2,3,4,5,6]
输出:6

示例 2:

输入:root = []
输出:0

示例 3:

输入:root = [1]
输出:1

提示:

  • 树中节点的数目范围是[0, 5 * 104]
  • 0 <= Node.val <= 5 * 104
  • 题目数据保证输入的树是 完全二叉树

进阶:遍历树来统计节点是一种时间复杂度为 O(n) 的简单解决方案。你可以设计一个更快的算法吗?

自己看到题目的第一想法

    我又忘记了完全二叉树的定义...

看完代码随想录之后的想法

    完全二叉树就是除了最后一层, 其他所有层的节点都是满的. 同时最后一层所有的叶子结点, 是从左到右依次排开, 不能产生中断. 因此完全二叉树可以通过递归的方式, 慢慢分解为满二叉树.

/**
 * 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;
        }
        int leftSizeCount = 0;
        int rightSizeCount = 0;
        TreeNode leftNode = root.left;
        TreeNode rightNode = root.right;
        while (leftNode != null) {
            leftSizeCount++;
            leftNode = leftNode.left;
        }
        while (rightNode != null) {
            rightSizeCount++;
            rightNode = rightNode.right;
        }
        if (leftSizeCount == rightSizeCount) {
            return (2 << leftSizeCount) - 1;     
        } else {
            return countNodes(root.left) + countNodes(root.right) + 1;
        }
    }
}
// 递归法: 中序遍历
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) {
            return 0;
        }
        LinkedList<TreeNode> nodes = new LinkedList<>();
        TreeNode node = root;
        int count = 0;
        while (node != null || !nodes.isEmpty()) {
            if (node != null) {
                nodes.addLast(node);
                node = node.left;
            } else {
                node = nodes.pop();
                count++;
                node = node.right;
            }
        }
        return count;
    }
}

自己实现过程中遇到哪些困难

    大多都忘记了. 但是通过把完全二叉树降解为满二叉树求节点数量的时候, 终止条件有点没想明白. 2 << depth - 1 这样的写法会返回错误的值, 需要加一个括号: (2 << depth) - 1.

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值