Maximum Depth of N-ary Tree Solutions in C++
Number 559
Difficulty Easy
Acceptance 68.7%
Link LeetCode
Other languages —
Solutions
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description//// Author : liuyubobobo/// Time : 2018-10-03#include <iostream>#include <vector>using namespace std;/// DFS/// Time Complexity: O(n)/// Space Complexity: O(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 res = 1;for(Node* child: root->children)res = max(res, 1 + maxDepth(child));return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description//// Author : liuyubobobo/// Time : 2018-10-03#include <iostream>#include <vector>#include <stack>using namespace std;/// Using Stack - Non recursion////// Time Complexity: O(n)/// Space Complexity: O(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;stack<pair<Node*, int>> stack;stack.push(make_pair(root, 1));int res = 1;while(!stack.empty()){Node* cur = stack.top().first;int depth = stack.top().second;stack.pop();res = max(res, depth);for(Node* node: cur->children)stack.push(make_pair(node, depth + 1));}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/maximum-depth-of-n-ary-tree/description//// Author : liuyubobobo/// Time : 2018-10-03#include <iostream>#include <vector>#include <queue>using namespace std;/// BFS/// Time Complexity: O(n)/// Space Complexity: O(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;queue<pair<Node*, int>> q;q.push(make_pair(root, 1));int res = 1;while(!q.empty()){Node* cur = q.front().first;int depth = q.front().second;q.pop();res = max(res, depth);for(Node* node: cur->children)q.push(make_pair(node, depth + 1));}return res;}};int main() {return 0;}