Title Description
Given an N-tree to find its maximum depth.
The maximum depth is the total number of nodes on the farthest from the root node to the leaf node of the longest path.
For example, given a tree 3:
We should return to its maximum depth, 3.
Description:
Depth of the tree is not more than 1,000.
Node of the tree is never more than 5,000.
Code
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val) {
val = _val;
}
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;
}else{
int max = 0;//子树的最大高度
for(int i=0;i<root.children.size();i++){
int curr = maxDepth(root.children.get(i));
if(curr > max){
max = curr;
}
}
return max+1;
}
}
}
Performance