LeetCode算法题-Maximum Depth of N-ary Tree(Java实现)

从CCD数据上报看wxHOOK的封号风险与规避 本文深入分析了微信CCD数据上报机制对wxHOOK的检测逻辑与封号风险,揭示了调试器路径暴露、非白名单DLL加载和内存校验失败等关键风险点。通过实战案例和规避方案,帮助开发者理解如何安全使用wxHOOK技术,避免封号风险,包括内存镜像欺骗、内核级隐蔽和硬件辅助方案等高级技巧。 阅读详情

这是悦乐书的第261次更新,第274篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第128题(顺位题号是559)。给定n-ary树,找到它的最大深度。最大深度是从根节点到最远叶节点的最长路径上的节点数。例如,给定一个3-ary树:

我们应该返回它的最大深度,即3。

注意:

  • 树的深度最多为1000。

  • 节点总数最多为5000。

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

这道题与求二叉树的最大深度是类似的,只不过二叉树是只有左右子节点,而这个n-ary树的子节点是一个数组,含有多个节点,所以我们只需要将左右子节点替换一下,变成for循环来处理,数组中包含的节点其实就是该问题的一个子问题,所以我们使用递归,调用方法自身,最后返回一个最大值即可。

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val,List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {

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

03 第二种解法

我们也可以使用迭代的方式来处理。我们使用队列先将根节点放入队列,然后开始循环处理,先将上一次放入队列的数据处理完,因此需要再使用一层循环,此层循环是处理每层的数据,因为其子节点是一个数组,还需要再使用一个循环来将新的节点放入队列,所以一共是三层循环,最后返回其深度即可。

/*
// Definition for a Node.
class Node {
    public int val;
    public List<Node> children;

    public Node() {}

    public Node(int _val,List<Node> _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
    public int maxDepth(Node root) {
        if (root == null) {
            return 0;
        }
        if (root.children == null) {
           return 1; 
        }
        Queue<Node> queue = new LinkedList<Node>();
        queue.offer(root);
        int depth = 0;
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i=0; i<size; i++) {
                Node temp = queue.poll();
                for (Node n : temp.children) {
                    queue.offer(n);
                }
            }
            depth++;
        }
        return depth;
    }
}

04 小结

算法专题目前已日更超过三个月,算法题文章128+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

Raspberry Pi Pico SDK开发-Timer-定时 RP2040 由 Raspberry Pi 设计,具有双核 Arm Cortex-M0+ 处理器和 264KB 内部 RAM,并支持高达 16MB 的片外闪存。 广泛的灵活 I/O 选项包括 I2C、SPI 和独特的可编程 I/O (PIO)。 本实例将演示如何使用Timer的定时功能。 阅读详情

相关推荐

YOLOv8的Anchor-Free革新:从中心点预测到动态IoU匹配的全面解析

本文深入解析了YOLOv8采用Anchor-Free(无锚框)设计的核心优势与实现机制。通过摒弃预设锚框,模型直接预测目标中心点与宽高,结合解耦头、动态IoU匹配及Varifocal Loss等创新,显著简化了训练流程并提升了检测精度,尤其在小目标检测和模型泛化能力上表现突出。

weixin_29094301的博客 358

559. Maximum Depth of N-ary Tree

计算一颗多叉树的最大深度。

不积跬步无以至千里 932

Windows 24H2下eNSP AR_40启动失败的终极修复指南

本文针对Windows 11 24H2更新后eNSP AR_40路由器启动失败的问题,提供了终极修复方案。核心在于解决系统强制虚拟化安全(VBS/Hyper-V)与老版模拟软件的兼容性冲突。指南详细介绍了安装关键系统更新KB5053656、精细调整Windows虚拟化功能以及后续排错步骤,帮助用户在24H2系统上稳定运行eNSP进行网络实验。

weixin_29233333的博客 508

【java】559. Maximum Depth of N-ary Tree

原文链接https://leetcode-cn.com/problems/maximum-depth-of-n-ary-tree/description/ 这道题的方法对很多二叉树的问题都十分试用,逻辑很简单,就是一层一层遍历。 public int maxDepth(Node root) { if(root == null) return 0 ; int r...

amber804105的博客 2096

【AutoDL】使用云服务器跑深度学习代码

1.算力市场 租用服务器,选择自己心仪的服务器2.镜像可以选择一些基础的镜像,社区镜像里是git上有的环境。3.上传文件在文件存储中,选择上传的区,在右边点击上传,选择自己的文件上传到云服务器中。

weixin_45768644的博客 3705

AI agents在公司财务报表分析中的应用

在当今竞争激烈的商业环境中,公司财务报表分析对于企业的决策制定、风险评估、战略规划等方面起着至关重要的作用。传统的财务报表分析方法往往依赖于人工操作,存在效率低下、容易出错、难以处理海量数据等问题。而AI agents作为人工智能领域的重要成果,具有自动化、智能化、高效性等特点,能够为财务报表分析带来新的突破和变革。

SuperAGI2025 1612

LeetCode-Java-559. Maximum Depth of N-ary Tree

题目 Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a 3-ary tree: ...

咸鱼塘 465

ncode控件使用总结

数据格式txt转s3t #HEADER #TITLES Absolute_Time,COG_Ax,COG_Ay,COG_Az,LF_SPINDLE_AZ #UNITS ,g,g,g,g, #DATATYPES Double,Float,Float,Float,Float #DATA 2. 导入s3t数据 3. 输入数据控件 直接拖入数据源或者添加控件数据 4.

技术联盟 2007

【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python)

【LeetCode】559. Maximum Depth of N-ary Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/maxi...

负雪明烛 3294

UE4打包为Framework库嵌入iOS原生工程

UE4打包为Framework库嵌入iOS原生工程

tangfuling1991的博客 1294

数据分享|2023年OSM道路数据(全国,分省,分市)

首先这个工作非常有必要的,因为我们正常使用的道路的分类都是基于包括城市快速路、城市主干路、城市次干路这个体系的,大家对于这个分类体系应该更熟悉,但是OSM地图的道路分类又不是完全按这个来的,所以非常有必要重新分类;搞清楚道路类型是我们使用道路数据做很多工作的基础,比如我们在使用道路数据的时候,经常需要预估道路的行车速度,而这个速度一般就是根据道路类型来的,比如高架路的车速肯定就比城市支路快很多!下载分为两种情况,第一种是下载小区域的数据,比如下载一个街区的数据,第二种为下载整个中国范围的。

JData_Engineer的博客 4307

【Leetcode】559. Maximum Depth of N-ary Tree

题目地址: https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ 求多叉树的高度。用分治法。 import java.util.List; public class Solution { public int maxDepth(Node root) { if (root == null) { ...

数学、算法爱好者的博客 174

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

Leetcode 559.N叉树的最大深度 Maximum Depth of N-ary Tree(Java)

Leetcode 559.N叉树的最大深度 Maximum Depth of N-ary Tree(Java) ##Tree##, ##Depth-first Search##, ##Breadth-first Search## N叉树的最大深度 采用DFS,类似二叉树求最大深度的方式,采用后序遍历的方式,递归处理所有结点 叶子结点的深度记为1 递归计算当前结点的子节点到叶子结点的最大深度max 考虑到当前结点未被计算深度,返回max + 1 时间复杂度: O(n) class Soluti

qq_42411921的博客 192

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

LeetCode - Easy - 559. Maximum Depth of N-ary Tree

Topic Tree Depth-first Search Breadth-first Search Description https://leetcode.com/problems/maximum-depth-of-n-ary-tree/ Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down

KISS 237

LeetCode 559: N 叉树的最大深度 Maximum Depth of N-ary Tree

题目: 给定一个 N 叉树,找到其最大深度。Given a n-ary tree, find its maximum depth.最大深度是指从根节点到最远叶子节点的最长路径上的节点总数...

爱写Bug 318

【leetcode】559. Maximum Depth of N-ary Tree

/* // Definition for a Node. class Node { public int val; public List<Node> children; public Node() {} public Node(int _val,List<Node> _children) { val = _val...

Art1st_D的博客 363

559. Maximum Depth of N-ary Tree - LeetCode

为什么80%的码农都做不了架构师?>>> ...

weixin_34247299的博客 92

Leetcode 559. Maximum Depth of N-ary Tree(DFS与BFS)

Leetcode 559. Maximum Depth of N-ary Tree 题目链接: Maximum Depth of N-ary Tree 难度:Easy 题目大意: 求N叉树的最大深度。 思路: 思路1(DFS): 利用递归,如果根节点非空,N叉树的最大深度等于所以子节点的最大深度的最大值再加一。但这种方法写出来的代码最后超时了。 思路2(BFS): 利用迭代,每次把一层的节点全部出队,再把下一层的节点全部入队,统计有多少层。 代码 思路1代码 /* // Definition for a

xue1236的博客 253

LeetCode-Maximum Depth of N-ary Tree

LeetCode Java Maximum Depth of N-ary Tree

坟头草一米七五 275

(Java)leetcode-559 Maximum Depth of N-ary Tree (N叉树的最大深度)

求树的最大深度!

不思量 333

LeetCode-559. Maximum Depth of N-ary Tree

Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a 3-ary tree:   ...

Burette_Lee的博客 724

LeetCode 104 -Maximum Depth of Binary Tree ( JAVA )

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. /** * Definition for a bina

UckyK的专栏 1099

[LeetCode] Maximum Depth of N-ary Tree N叉树的最大深度

Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. For example, given a3-arytree: ...

weixin_30567471的博客 144

Leetcode 559. Maximum Depth of N-ary Tree

问题描述: Given a n-ary tree, find its maximum depth. 求多叉树的最大深度 思路: 求深度的问题一般用递归: 某一个节点处的深度=其所有子节点的最大深度+1 代码如下: class Solution { public int maxDepth(Node root) { //base case: if the root is null, the tree doesn't exist -> its height is 0 if(r

weixin_51401145的博客 194
上一篇: LeetCode算法题-Quad Tree Intersection(Java实现)
下一篇: LeetCode算法题-Array Partition I(Java实现)
程序员小川
博客等级 码龄10年 47粉丝 305原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值