#590
Easy Algorithms N ary tree postorder traversal
Stack Tree Depth-First Search
81.0% acceptance
Jan 13, 2026
2754
119
Given the root of an n-ary tree, return the postorder traversal of its nodes' values.
Solution
C++
Time O(n)
Space O(1)
class Solution {
public:
vector<int> postorder(Node* root) {
if (!root) return {};
vector<int> result;
for (Node* child : root->children) {
auto sub = postorder(child);
result.insert(result.end(), sub.begin(), sub.end());
}
result.push_back(root->val);
return result;
}
};