自学内容网 自学内容网

从零开始的LeetCode刷题日记:559. N 叉树的最大深度

一.相关链接

题目链接:559. N 叉树的最大深度

二.心得体会

这道题和二叉树的没有本质区别,只是要遍历每一个子树,求出最大值即可。

三.代码
class Solution {
public:
    int maxDepth(Node* root) {
        if(root==NULL) return 0;
        int max = 0;
        int size = root->children.size();
        for(int i=0;i<size;i++) {
            int deep = maxDepth(root->children[i]);//遍历每个子树
            if(deep>max) max = deep;
        }
        return max+1;
    }
};


原文地址:https://blog.csdn.net/chenjialehhh/article/details/143077890

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!