算法跟学Day16【代码随想录】

文章介绍了如何计算二叉树和N叉树的最大深度及最小深度,提供了递归和迭代两种方法。在二叉树中,最大深度是所有路径中最长的,最小深度是最短路径。对于N叉树,计算方式类似。复杂度均为O(n),其中n是树的节点数量。
摘要由CSDN通过智能技术生成

第六章 二叉树part03

大纲

● 104.二叉树的最大深度 559.n叉树的最大深度
● 111.二叉树的最小深度
● 222.完全二叉树的节点个数

leetcode 104 二叉树的最大深度

思路

  • 递归法除空节点外直接返回最大深度+1
  • 迭代法借助队列逐层遍历 记录遍历的深度

细节

代码

class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        else return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
    }
}
class Solution {
    public int maxDepth(TreeNode root) {
        if (root == null) return 0;
        int depth = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
            depth++;
        }
        return depth;
    }
}

复杂度

  • 时间O(n)
  • 空间O(n)

leetcode 559 N 叉树的最大深度

思路

  • 思路同求二叉树的最大深度

细节

代码

class Solution {
    public int maxDepth(Node root) {
        int depth = 0;
        if (root == null) return depth;
        for (Node child : root.children) depth = Math.max(depth, maxDepth(child));
        return 1 + depth;
    }
}
class Solution {
    public int maxDepth(Node root) {
        int depth = 0;
        if (root == null) return depth;
        Queue<Node> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            while (size-- > 0) 
                for (Node child : queue.poll().children)
                    if (child != null) queue.offer(child);
            depth++;
        }
        return depth;
    }
}

复杂度

  • 时间O(n)
  • 空间O(n)

leetcode 111 二叉树的最小深度

思路

  • 递归法有条件的递归返回
  • 迭代法使用队列记录最小深度

细节

  • 注意最小深度的判定条件应为左右子结点均为空出现的结点

代码

class Solution {
    public int minDepth(TreeNode root) {
        if (root == null) return 0;
        if (root.left != null && root.right == null) return 1 + minDepth(root.left);
        if (root.left == null && root.right != null) return 1 + minDepth(root.right);
        return 1 + Math.min(minDepth(root.left), minDepth(root.right));
    }
}
class Solution {
    public int minDepth(TreeNode root) {
        int depth = 0;
        if (root == null) return depth;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            depth++;
            while (size-- > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
                if (node.left == null && node.right == null) return depth;
            }
        }
        return depth;
    }
}

复杂度

  • 时间O(n)
  • 空间O(n)

leetcode 222

思路

  • 迭代法可利用慢二叉树性质进行迭代

细节

代码

class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        return 1 + countNodes(root.left) + countNodes(root.right);
    }
}
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int ld = getDepth(root.left), rd = getDepth(root.right);
        if (ld == rd) return countNodes(root.right) + (1 << ld);
        else return countNodes(root.left) + (1 << rd);
    }
    public int getDepth(TreeNode node) {
        if (node == null) return 0;
        return 1 + getDepth(node.left);
    }
}
class Solution {
    public int countNodes(TreeNode root) {
        if (root == null) return 0;
        int count = 0;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            count += size;
            while (size-- > 0) {
                TreeNode node = queue.poll();
                if (node.left != null) queue.offer(node.left);
                if (node.right != null) queue.offer(node.right);
            }
        }
        return count;
    }
}

复杂度

  • 时间O(n)
  • 空间O(n)
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值