天天看點

LeetCode_559_N叉樹的最大深度

題目描述:

LeetCode_559_N叉樹的最大深度
/*
// 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:
    void DFStraverse(Node* root,int curdepth,int &maxdepth){
        if(root==NULL)
            return ;
        int size=root->children.size();
        if(size==0){//葉子節點
            if(curdepth>maxdepth)
                maxdepth=curdepth;
        }
        for(int i=0;i<size;i++){
            DFStraverse(root->children[i],curdepth+1,maxdepth);
        }    
    }
    
    int maxDepth(Node* root) {
        if(root==NULL)
            return 0;
        int maxdepth=0;
        DFStraverse(root,1,maxdepth);
        return maxdepth;
    }
};