590. N-ary Tree Postorder Traversal

class Solution {
public:
    vector postorder(Node* root) {
        vector ret;
        if (!root) 
            return ret;
        stack s;
        s.push(root);
        Node *cur; 
        while (!s.empty())
        {
            cur = s.top();
            s.pop();
            ret.insert(ret.begin(), cur->val);
            for (int i = 0; i < cur->children.size(); ++i)
            {
                if (cur->children[i])
                    s.push(cur->children[i]);
            }
        }
        
        return ret;
    }
};

你可能感兴趣的:(590. N-ary Tree Postorder Traversal)