题目:
题解:
/*** Definition for a Node.* struct Node {* int val;* int numChildren;* struct Node** children;* };*/int maxDepth(struct Node* root) {if (!root) {return 0;}int depth = 0;// 创建空队列const int qCap = 10e4 + 1;struct Node **q = (struct Node **)malloc(sizeof(struct Node*) * qCap);int front = 0, rear = 0;// 队列初始化q[rear] = root;rear = (rear + 1) % qCap;int qSize = (rear - front + qCap) % qCap;while (qSize != 0) {while (qSize > 0) {struct Node *n = q[front];front = (front + 1) % qCap;struct Node **children = n->children;for (int i = 0; i < n->numChildren; i++) {q[rear] = children[i];rear = (rear + 1) % qCap;}qSize--;}qSize = (rear - front + qCap) % qCap;depth++;}free(q);return depth;
}