1. 程式人生 > >559. Maximum Depth of N-ary Tree C++

559. Maximum Depth of N-ary Tree C++

很簡單的深度優先搜尋

設定好遞迴返回的條件和值

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

    Node() {}

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    int maxDepth(Node* root) {
        
if(!root) return 0; int res = 1; for(Node* child : root->children) res = max(res,maxDepth(child)+1); return res; } };