N 叉树的最大深度

N 叉树的最大深度

class Solution {
    
    
public:
    int maxDepth(Node* root) {
    
    
        if(!root) return 0;
      return depth(root);
        
    }
    int depth(Node* root)
    {
    
    
        if(!root) return 0;
        int max1=0;
        for(int i=0;i<root->children.size();i++)
        {
    
    
          int cdepth=depth(root->children[i]);
           max1=max(max1,cdepth);//选所有孩子中高度最大的
        }
        return max1+1;//然后加上自己的高度
        
    }
};

猜你喜欢

转载自blog.csdn.net/qq_44808694/article/details/111472184
今日推荐