leetcode题解-559-N叉树的最大深度 发表于 2019-12-05 | 更新于 2019-12-10 | 分类于 刷题 题目 My way 递归 12345678910111213141516171819202122232425262728293031323334/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }};*/class Solution {public: int maxDepth(Node* root) { if (!root) return 0; if (root->children.empty()) return 1; vector<int> Height; for (auto it : root->children) { Height.push_back(maxDepth(it)); } return *max_element(Height.begin(), Height.end()) + 1; }}; 手动压栈DFS 123456789101112131415161718192021222324252627282930313233343536373839404142434445/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }};*/class Solution {public: int maxDepth(Node* root) { if (!root) return 0; if (root->children.empty()) return 1; stack<pair<Node*, int>> stack; stack.push(make_pair(root, 1)); int max_depth = -1; while(!stack.empty()) { auto top = stack.top().first; int depth = stack.top().second; max_depth = max(max_depth, depth); stack.pop(); for (auto it : top->children) { stack.push(make_pair(it, depth+1)); } } return max_depth; }}; 层次遍历 123456789101112131415161718192021222324252627282930313233343536373839404142434445/*// Definition for a Node.class Node {public: int val; vector<Node*> children; Node() {} Node(int _val) { val = _val; } Node(int _val, vector<Node*> _children) { val = _val; children = _children; }};*/class Solution {public:int maxDepth(Node* root) { if (!root) return 0; queue<Node*> queue; queue.push(root); int max_depth = 0; while (!queue.empty()) { max_depth++; for (int size = queue.size(); size; size--) { Node* curr = queue.front(); queue.pop(); for (Node* it : curr->children) { queue.push(it); } } } return max_depth;}};