代码随想录训练营day16

目录

题目一:二叉树的最大深度

解法一:递归

解法二:迭代

题目二:二叉树的最小深度

解法一:递归

解法二:迭代 

题目三:完全二叉树的节点个数

解法一:递归

解法二:迭代


题目一:二叉树的最大深度

力扣题目链接

题目描述:给定一个二叉树,找出其最大深度。

思路分析:(详情见代码随想录

根节点的高度就是二叉树的最大深度,采用后序遍历

解法一:递归


class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0; // 叶子节点的高度为一,空节点高度则为0
        int leftDepth = maxDepth(root.left);
        int rightDepth = maxDepth(root.right);
        // 处理逻辑放在最后,可以把叶子节点的高度返回给它的父节点,进行加一操作,可得出父节点的高度
        return Math.max(leftDepth, rightDepth) + 1; 
    }
}

解法二:迭代


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

题目二:二叉树的最小深度

力扣题目链接

题目描述:给定一个二叉树,找出其最小深度

思路分析:

最小深度是从根节点到最近叶子节点的最短路径上的节点数量,找到最低点是解题关键

解法一:递归


class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        int leftDepth = minDepth(root.left); // 左
        int rightDepth = minDepth(root.right); // 右
        // 中
        // 当一个左子树为空,右不为空,这时并不是最低点
        if (root.left == null) {
            return rightDepth + 1;
        }
        // 当一个右子树为空,左不为空,这时并不是最低点
        if (root.right == null) {
            return leftDepth + 1;
        }
        // 左右节点都不为空 
        return Math.min(leftDepth, rightDepth) + 1;
    }
}

解法二:迭代 


class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        int depth = 0;
        while (!deque.isEmpty()) {
            int size = deque.size();
            depth++;
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                if (node.left == null && node.right == null) { // 叶子节点直接返回depth
                    return depth;
                }
                if (node.left != null) {
                    deque.offer(node.left);
                }
                if (node.right != null) {
                    deque.offer(node.right);
                }
            }
        }
        return depth;
    }
}

题目三:完全二叉树的节点个数

力扣题目链接

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

思路分析:(视频指路

先搞清楚完全二叉树的定义,采用递归法、迭代法解题即可

解法一:递归

// 精简版
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        return countNodes(root.left) + countNodes(root.right) + 1;
    }
}

解法二:迭代


class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        Deque<TreeNode> deque = new LinkedList<>();
        deque.offer(root);
        int res = 0;
        while (!deque.isEmpty()) {
            int size = deque.size();
            while (size > 0) {
                TreeNode cur = deque.poll();
                res++;
                if (cur.left != null) deque.offer(cur.left);
                if (cur.right != null) deque.offer(cur.right);
                size--;
            }
        }
        return res;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值