[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:

We should return its max depth, which is 3.

Note:

  1. The depth of the tree is at most 1000.
  2. The total number of nodes is at most 5000.

Solution 1. Recursion

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public List<Node> children;
 6
 7     public Node() {}
 8
 9     public Node(int _val,List<Node> _children) {
10         val = _val;
11         children = _children;
12     }
13 };
14 */
15 class Solution {
16     public int maxDepth(Node root) {
17         if (root == null) {
18             return 0;
19         }
20         int max = 0;
21         for(Node child : root.children) {
22             max = Math.max(maxDepth(child), max);
23         }
24         return max + 1;
25     }
26 }

Solution 2. BFS(tracking the number of nodes that need to be visited at each level)

 1 class Solution {
 2     public int maxDepth(Node root) {
 3         if(root == null) {
 4             return 0;
 5         }
 6         int level = 0;
 7         int currLevelCount = 1;
 8         int nextLevelCount = 0;
 9         Queue<Node> queue = new LinkedList<>();
10         queue.add(root);
11         while(!queue.isEmpty()) {
12             Node currNode = queue.poll();
13             currLevelCount--;
14             for(Node child : currNode.children) {
15                 queue.add(child);
16                 nextLevelCount++;
17             }
18             if(currLevelCount == 0) {
19                 level++;
20                 currLevelCount = nextLevelCount;
21                 nextLevelCount = 0;
22             }
23         }
24         return level;
25     }
26 }

Follow up:  Can you implement a BFS to a certain depth?

原文地址:https://www.cnblogs.com/lz87/p/9937842.html

时间: 2024-10-29 12:51:50

[LeetCode] 559. Maximum Depth of N-ary Tree的相关文章

【LeetCode】 Maximum Depth of Binary Tree

Maximum Depth of Binary Tree  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. 递归基础,不解释. class Solution { public: int getMax(TreeNode *root

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree 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. class Solution { public: int maxDepth(TreeNode *root) { inM

Leetcode 树 Maximum Depth of Binary Tree

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Maximum Depth of Binary Tree Total Accepted: 16605 Total Submissions: 38287 Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the ro

[LeetCode&amp;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 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. Note: The dept

[LeetCode][Java]Maximum Depth of Binary Tree

https://leetcode.com/problems/maximum-depth-of-binary-tree/ Maximum Depth of Binary Tree 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 1

[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 longest path from the root node down to the farthest leaf node. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

leetcode 104 Maximum Depth of Binary Tree二叉树求深度

Maximum Depth of Binary Tree Total Accepted: 63668 Total Submissions: 141121 My Submissions Question Solution 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

[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. 题意及分析:找出一棵树的高度,即最深子节点.使用深度遍历的方法即可,用一个变量记录遍历到当前点的最大高度,然后当前点若有子节点,遍历到子节点,那么该点的高度+1和当前的最大

leetcode之Maximum Depth of Binary Tree 以及Balanced Binary Tree

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. 二叉树的深度,用递归特别方便! 代码如下: public int maxDepth(TreeNode root) { if(root==null){ return 0; } int