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:
559. Maximum Depth of N-ary Tree_第1张图片
We should return its max depth, which is 3.

Note:

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

class Node {
public:
    int val;
    vector children;

    Node() {}

    Node(int _val, vector _children) {
        val = _val;
        children = _children;
    }
};

class Solution {
public:
    int maxDepth(Node* root) {
        if(!root){
            return 0;
        }
        int depth = 0;

        for(vector::iterator iter = root->children.begin(); iter != root->children.end(); ++iter){
            int r = maxDepth(*iter);
            if(r > depth){
                depth = r;
            }
        }
        return depth + 1;
    }
};

你可能感兴趣的:(Leetcode)