Day 16 | 二叉树 104. 二叉树的最大深度 / 559. N 叉树的最大深度 / 111. 二叉树的最小深度 / 222. 完全二叉树的节点个数

Leetcode 104. 二叉树的最大深度

题目链接:104. 二叉树的最大深度

二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)

二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)

本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。

代码实现(递归法)

class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        return 1 + Math.max(leftHeight, rightHeight);
    }
}

当然层序遍历也可以,而且更简单。在层序遍历那篇博客中,也记录了这道题。

代码实现(迭代法)

class Solution {
    public int maxDepth(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        if(root != null){
            deque.add(root);
        }
        int depth = 0;
        while(!deque.isEmpty()){
            int size = deque.size();
            depth++;
            for(int i = 1; i <= size; i++){
                TreeNode node = deque.poll();
                if(node.left != null){
                    deque.add(node.left);
                }
                if(node.right != null){
                    deque.add(node.right);
                }
            }
        }
        return depth;        
    }
}

Leetcode 559. N 叉树的最大深度

题目链接:559. N 叉树的最大深度

代码实现(递归法)

class Solution {
    public int maxDepth(Node root) {
        if(root == null){
            return 0;
        } 
        int depth = 0;
        for(int i = 0; i < root.children.size(); i++){
            depth = Math.max(depth, maxDepth(root.children.get(i)));
        }
        return depth + 1;
    }
}

代码实现(迭代法)

class Solution {
    public int maxDepth(Node root) {
        Deque<Node> deque = new LinkedList<>();
        if(root != null){
            deque.add(root);
        }
        int depth = 0;
        while(!deque.isEmpty()){
            int size = deque.size();
            depth++;
            for(int i = 1; i <= size; i++){
                Node node = deque.poll();
                List<Node> children = node.children;
                if(children == null || children.size()== 0){
                    continue;
                }
                for(Node child : children){
                    if(child != null){
                        deque.add(child);
                    }             
                }
            }
        }
        return depth;        
    }
}

Leetcode 111. 二叉树的最小深度

题目链接:111. 二叉树的最小深度

和上一题的求最大深度看似差不多,其实差距还挺大,有坑。

本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。

代码实现(递归法)

class Solution {
    public int minDepth(TreeNode root) {
        if(root == null){
            return 0;
        } 
        int leftHeight = minDepth(root.left);
        int rightHeight = minDepth(root.right);
        if(root.left == null && root.right != null){
            return rightHeight + 1;
        }
        if(root.left != null && root.right == null){
            return leftHeight + 1;
        }
        return Math.min(leftHeight, rightHeight) + 1;
    }
}

代码实现(迭代法)

class Solution {
    public int minDepth(TreeNode root) {
        Deque<TreeNode> deque = new LinkedList<>();
        if(root != null){
            deque.add(root);
        }
        int depth = 0;
        while(!deque.isEmpty()){
            int size = deque.size();
            depth++;
            for(int i = 1; i <= size; i++){
                TreeNode node = deque.poll();
                if(node.left == null && node.right == null){
                    return depth;
                }
                if(node.left != null){
                    deque.add(node.left);
                }
                if(node.right != null){
                    deque.add(node.right);
                }
            }
        }
        return depth;        
    }
}

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

题目链接:222. 完全二叉树的节点个数

本题要利用完全二叉树的性质。

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。

对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。
对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。

代码实现

class Solution {
    public int countNodes(TreeNode root) {
        if(root == null){
            return 0;
        }
        int leftDepth = 1;
        int rightDepth = 1;
        TreeNode left = root.left;
        TreeNode right = root.right;
        while(left != null){
            left = left.left;
            leftDepth++;
        }
        while(right != null){
            right = right.right;
            rightDepth++;
        }
        if(leftDepth == rightDepth){
            return (1 << leftDepth) - 1; 
        }
        int leftNodes = countNodes(root.left);
        int rightNodes = countNodes(root.right);
        return leftNodes + rightNodes + 1;
    }
}
  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值