代码随想录day16 Java版

本文介绍了在LeetCode中解决关于二叉树问题的四种方法:计算最大深度采用递归和层序遍历,最小深度有递归和层序两种版本,以及计算完全二叉树节点个数的简单遍历。
摘要由CSDN通过智能技术生成

最近要搞论文了,课程也比较忙,拖了几天。

力扣104 二叉树最大深度

思路简单,层序或者递归做都行

// 递归
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        return dfs(root);
    }
    public int dfs(TreeNode root) {
        if (root == null) return 0;
        return Math.max(dfs(root.left), dfs(root.right)) + 1;
    }
}
// 层序
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int depth = 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            int size = q.size();
            while (size -- > 0) {
                TreeNode temp = q.poll();
                if (temp.left != null) q.add(temp.left);
                if (temp.right != null) q.add(temp.right);
            }
            depth ++;
        }
        return depth;
    }
}

力扣111 二叉树的最小深度

// 层序
class Solution {
    public int minDepth(TreeNode root) {
        int depth = 1;
        if (root == null) return 0;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()) {
            int size = q.size();
            while (size -- > 0) {
                TreeNode temp = q.poll();
                if (temp.left == null && temp.right == null) return depth;
                if (temp.left != null) q.add(temp.left);
                if (temp.right != null) q.add(temp.right);
            }
            depth ++;
        }
        return depth;
    }
}
// 递归
class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        return dfs(root);
    }
    public int dfs(TreeNode root) {
        if (root == null) return 100000;
        else if (root.left == null && root.right == null) return 1;
        return Math.min(dfs(root.left), dfs(root.right)) + 1;
    }
}
// 正常递归
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 1 + leftHeight;
        }
        if (root.right != null && root.left == null) {
            return 1 + rightHeight;
        }
        return 1 + Math.min(leftHeight, rightHeight);
    }
}

力扣222 完全二叉树的节点个数

每遍历到一个节点就加一即可,选择什么遍历方式都可。

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int leftCount = countNodes(root.left);
        int rightCount = countNodes(root.right);
        return leftCount + rightCount + 1;
    }
}

  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值