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:
int maxDepth(Node* root) {
if(!root) return 0;
int max = 0;
vector<int> depths(root->children.size());//子树深度
for(int i = 0; i < depths.size(); i++) {
depths[i] = maxDepth(root->children[i]);//记录
}
for(int depth : depths) {//找出子树中最大的深度
if(depth > max) max = depth;
}
return max + 1;//当前树深度为子树深度加1
}
};
思路:
- 先序遍历,跟二叉树求深度思路相同,先对每棵子树求深度,对于父结点为根的树,它的深为子树的深加1。
- 唯一的区别在于,N叉树的子树数目是不确定的,需要使用动态分配的空间来记录。
- 也可以使用层序遍历,不过需要额外的栈,没有必要。
2019/06/1 18:39