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:

We should return its max depth, which is 3.

  1. /*
  2. // Definition for a Node.
  3. class Node {
  4. public:
  5. int val;
  6. vector<Node*> children;
  7.  
  8. Node() {}
  9.  
  10. Node(int _val, vector<Node*> _children) {
  11. val = _val;
  12. children = _children;
  13. }
  14. };
  15. */
  16. class Solution {
  17. public:
  18. int maxDepth(Node* root) {
  19. if (root == NULL) {
  20. return ;
  21. }
  22.  
  23. int maxd = ;
  24. int tmp = ;
  25. for (auto child : root->children)
  26. {
  27. tmp = + maxDepth(child);
  28. if (tmp > maxd) {
  29. maxd = tmp;
  30. }
  31. }
  32.  
  33. return maxd;
  34. }
  35.  
  36. // int maxDepth(Node* root) {
  37. // if (root == nullptr) return 0;
  38. // int depth = 0;
  39. // for (auto child : root->children) depth = max(depth, maxDepth(child));
  40. // return 1 + depth;
  41. // }
  42. };

LeetCode 559. Maximum Depth of N-ary Tree(N-Tree的深度)的更多相关文章

  1. (N叉树 DFS 递归 BFS) 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 longe ...

  2. 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 ...

  3. 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 longe ...

  4. [LeetCode] 559. Maximum Depth of N-ary Tree_Easy tag: DFS

    Given a n-ary tree, find its maximum depth. The maximum depth is the number of nodes along the longe ...

  5. [leetcode] 559. Maximum Depth of N-ary Tree (easy)

    原题链接 思路: 简单bfs class Solution { public: int maxDepth(Node *root) { int depth = 0; if (root == NULL) ...

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

    Question 559. Maximum Depth of N-ary Tree Solution 题目大意:N叉树求最大深度 思路:用递归做,树的深度 = 1 + 子树最大深度 Java实现: / ...

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

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS BFS 日期 题目地址:https://le ...

  8. [LeetCode&Python] Problem 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 longe ...

  9. LeetCode 104. Maximum Depth of Binary Tree (二叉树的最大深度)

    Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

随机推荐

  1. python 监听键盘输入

    import sys, select, tty, termios old_attr = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fil ...

  2. python笔记43-加解密AES/CBC/pkcs7padding

    前言 有些公司对接口的安全要求比较高,传参数的时候,不会明文的传输,先对接口加密,返回的数据也加密返回. 目前比较常见的加密方式是AES/CBC/pkcs7padding. AES五种加密模式 在AE ...

  3. 浅析数据库(DB)、操作数据存储(ODS)和数据仓库(DW)的区别与联系

    文章背景: 相信大部分刚接触上面三个概念的同学,都多多少少会有些迷惑,现在我就给大家简单分析下这三者的关系,希望大家对这三者的概念理解有所帮助吧. 本文主要从下面两类关系来叙述上面三者的关系: 数据库 ...

  4. 同时引入依赖:spring-cloud-starter-gateway 和 spring-boot-starter-web,报错

    报错: 2019-09-19 11:19:21.437 WARN 72057 --- [ main] GatewayClassPathWarningAutoConfiguration : ****** ...

  5. Ruby for

    #!/usr/bin/ruby -w# -*- coding: UTF-8 -*-for i in 1..5 print i," "endprint "\n"f ...

  6. 本地项目git到github上

    步骤: 1.下载git,安装完成后到桌面右击鼠标会出现git的选项 2.创建一个本地仓库用来存储你的本地项目,我在D盘创建一个reposity的文件夹 3.在reposity文件夹打开git命令行,输 ...

  7. 硬币游戏2&&Cutting Game——Grundy值

    Grundy值 当前状态的Grundy值就是除任意一步所能转移到的状态的Grundy值以外的最小非负整数, 以硬币问题一为例,可写成: int init_grundy() { sg[] = ; ;i ...

  8. (7)树莓派读物USB摄像头

    https://blog.csdn.net/qq_42403190/article/details/90453305 创建文件 camera.py 简单读取 #!/usr/bin/env python ...

  9. Hibernate学习:Exception in thread "main" java.lang.NullPointerException

    1.在学习Hibernate多对多关系的时候遇到了一下异常: 主函数出现了空指针异常: public static void testadd() { Session session = Hiberna ...

  10. ent 基本使用九 代码生成

    ent 提供了cli 工具,可以方便我们进行schema 以及代码生成,同时目前提供的cli已经够用了 安装 cli go get github.com/facebookincubator/ent/c ...