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;
};
By guozetang            Updated: 2020-09-19 13:02:30

results matching ""

    No results matching ""