559. Maximum Depth of N-ary Tree 發表於 2023-02-13 | 分類於 leetcode problemsolutiondfs1234567891011121314151617class Solution {public: int maxDepth(Node* root) { // dfs if(!root) return 0; // for binary tree // return 1+max(maxDepth(root->left), maxDepth(root->right)); // n-ary tree int depth=1, mx = 0; for(Node *t:root->children){ mx = max(mx, maxDepth(t)); } return 1+mx; }}; bfs12345678910111213141516171819class Solution {public: int maxDepth(Node* root) { if(!root) return 0; queue<Node*> q({root}); int depth = 0; while(!q.empty()){ int size = q.size(); for(int i=0;i<size;++i){ Node * p =q.front(); q.pop(); for(Node *t:p->children) q.push(t); } depth++; } return depth; }};