代码随想录day16 || 二叉树最大深度|| 最小深度 || 完全二叉树结点个数

104.二叉树的最大深度

思路

● 递归或层序遍历,两种方法

代码

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> queue = new ArrayDeque();
        int maxDepth = 0;
        queue.addLast(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            maxDepth++;
            while (size-- > 0) {
                TreeNode temp = queue.removeFirst();
                if (temp.left != null) queue.addLast(temp.left);
                if (temp.right != null) queue.addLast(temp.right);
            }
        }
        return maxDepth;
    }
}

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

559.n叉树的最大深度

思路

● 类似,递归或迭代

代码

class Solution {
    public int maxDepth(Node root) {
        if (root == null) return 0;
        int temp = 0;
        for (Node child : root.children) {
            temp = Math.max(temp, maxDepth(child));
        }
        return temp + 1;
    }
}

class Solution {
    public int maxDepth(Node root) {
        if (root == null) return 0;
        Deque<Node> queue = new ArrayDeque();
        int maxDepth = 0;
        queue.addLast(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            maxDepth++;
            while (size-- > 0) {
                Node temp = queue.removeFirst();
                for (Node child : temp.children) {
                    if (child != null) queue.addLast(child);
                }
            }
        }
        return maxDepth;
    }
}

111.二叉树的最小深度

思路

● 力扣题目链接
● 给定一个二叉树,找出其最小深度。
● 最小深度是从根节点到最近叶子节点的最短路径上的节点数量。

代码

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

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> queue = new ArrayDeque();
        int minDepth = 0;
        queue.addLast(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            minDepth++;
            while (size-- > 0) {
                TreeNode temp = queue.removeFirst();
                // 层序遍历,找到了叶子节点,那就找到了最小深度,直接返回
                if (temp.left == null && temp.right == null) return minDepth;
                if (temp.left != null) queue.addLast(temp.left);
                if (temp.right != null) queue.addLast(temp.right);
            }
        }
        return -1; // 程序其实根本不会走到这一步,前面一定可以找到叶子节点
    }
}

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

● 力扣题目链接
● 给出一个完全二叉树,求出该树的节点个数。

思路

● 掌握普通二叉树的迭代和递归解法
● 完全二叉树的递归解法

代码

// 时间复杂度O(n) 空间复杂度O(logn)
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
// 时间复杂度O(n) 空间复杂度O(n)
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> queue = new ArrayDeque();
        int count = 0;
        queue.addLast(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                TreeNode temp = queue.removeFirst();
                count++;
                if (temp.left != null) queue.addLast(temp.left);
                if (temp.right != null) queue.addLast(temp.right);
            }
        }
        return count;
    }
}
// 使用完全二叉树的性质,如果是满二叉树,节点个数是 2 ^ (height) - 1
// 如果不是满二叉树,不断递归左右孩子,遇到满二叉树再套公式计算
class Solution {
    public int countNodes(TreeNode root) {
        // 看是否是满二叉树,是就给结果,不是再继续递归
        if (root == null) return 0;
        TreeNode left = root.left;
        TreeNode right = root.right;
        int leftDepth = 0;
        int rightDepth = 0;
        while (left != null) {
            leftDepth++;
            left = left.left;
        }
        while (right != null) {
            rightDepth++;
            right = right.right;
        }
        // 是满二叉树
        if (leftDepth == rightDepth) {
            return (2 << (leftDepth)) - 1;
        }
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值