美文网首页
590.n-ary-tree-postorder-travers

590.n-ary-tree-postorder-travers

作者: Optimization | 来源:发表于2020-05-20 17:01 被阅读0次

栈、队列

class Solution {
public:
    vector<int> postorder(Node* root) {
        if(!root) return {};
        deque<int> q;
        stack<Node*> s;
        s.push(root);
        while(!s.empty()){
            Node* curNode = s.top();s.pop();
            q.push_front(curNode->val);
            for(auto it = curNode->children.begin();it != curNode->children.end();it++){
                s.push(*it);
            }
        }
        return vector<int>(q.begin(),q.end());
    }
};

相关文章

网友评论

      本文标题:590.n-ary-tree-postorder-travers

      本文链接:https://www.haomeiwen.com/subject/wxpnohtx.html