590. N-ary Tree Postorder Traver

2018-11-20  本文已影响0人  Jesson3264
class Solution {
public:
    vector<int> postorder(Node* root) {
        vector<int> ret;
        if (!root) 
            return ret;
        stack<Node *> 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;
    }
};
上一篇下一篇

猜你喜欢

热点阅读