2022.2.22 LeetCode —— 二叉树


一、今日刷题

1. 第七部分:二叉树 – 104. 二叉树的最大深度

跳转LeetCode

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。


答案代码

方法一:递归

package BinaryTree;

/**
 * @author: LYZ
 * @date: 2022/2/22 21:05
 * @description: 104. 二叉树的最大深度
 */
public class MaxDepth {
    public static void main(String[] args) {
        TreeNode root = new TreeNode(1, new TreeNode(2, new TreeNode(3), new TreeNode(4)), new TreeNode(2, new TreeNode(4), new TreeNode(3)));
        MaxDepth maxDepth = new MaxDepth();
        int ans = maxDepth.maxDepth(root);
        System.out.println(ans);
    }

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

        return max + 1;
    }
}

方法二:迭代
使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度。所以这道题的迭代法就是一道模板题,可以使用二叉树层序遍历的模板来解决的。

public int maxDepth(TreeNode root) {
    int ans = 0;
    if (root == null) {
        return 0;
    }

    Queue<TreeNode> queue = new LinkedList<>();
    queue.offer(root);
    while (!queue.isEmpty()) {
        int size = queue.size();
        ans++;
        for (int i = 1; i <= size; i++) {
            TreeNode node = queue.poll();
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
    }
    return ans;
}

2. 第七部分:二叉树 – 559. N 叉树的最大深度

跳转LeetCode

给定一个 N 叉树,找到其最大深度。

最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。

N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
在这里插入图片描述


答案代码

方法一:递归

package BinaryTree;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: LYZ
 * @date: 2022/2/22 21:25
 * @description: 559. N 叉树的最大深度
 */
public class MaxDepthN {
    public static void main(String[] args) {
        List<Node> children = new ArrayList<>();
        children.add(new Node(2));
        children.add(new Node(3));
        children.add(new Node(4));
        children.add(new Node(5));
        Node node = new Node(1, children);
        MaxDepthN maxDepthN = new MaxDepthN();
        int ans = maxDepthN.maxDepthN(node);
        System.out.println(ans);
    }

    public int maxDepthN(Node root) {
        if (root == null) {
            return 0;
        }
        int ans = 0;
        if (root.children != null) {
            int size = root.children.size();
            for (Node node : root.children) {
                ans = Math.max(ans, maxDepthN(node));
            }
        }

        return ans + 1;
    }
}

要体会最后的 return 语句为什么 ans + 1(考察对整个递归过程的理解)

方法二:迭代

public int maxDepthN(Node root) {
        Queue<Node> queue = new LinkedList<>();
        int ans = 0;
        if (root == null) {
            return 0;
        }
        queue.offer(root);
        while (!queue.isEmpty()) {
            ans++;
            int size = queue.size();
            for (int i = 1; i <= size; i++) {
                Node node = queue.poll();
                if (node.children != null) {
                    for (Node n : node.children) {
                        if (n != null) {
                            queue.offer(n);
                        }
                    }
                }
            }
        }
        return ans;
    }

在这两道题里迭代的效果都不如递归好(无论从执行用时还是内存消耗)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值