559. Maximum Depth of N-ary Tree

/* // 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 == nullptr) return 0; vector<int> max_among_children; for (auto i : root->children){ int res = maxDepth(i) + 1; max_among_children.push_back(res); } auto max = max_element(max_among_children.begin(), max_among_children.end()); return max; } };
Try inorder traversal iteratively.

Compile error.

Note here is an N-ary tree, not a binary tree anymore!

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.