Question
Given an n-ary tree, return the postorder traversal of its nodes' values.
For example, given a 3-ary
tree:
Difficulty:Easy
Category:Tree
Analyze
Solution
class Solution {
public:
vector<int> postorder(Node* root) {
if (!root) return {};
for (auto& c : root->children) postorder(c);
ans.emplace_back(root->val);
return ans;
}
private:
vector<int> ans;
};