Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- // Definition for a Node.
- class Node {
- public:
- int val;
- vector<Node*> children;
- Node() {}
- Node(int _val) {
- val = _val;
- }
- Node(int _val, vector<Node*> _children) {
- val = _val;
- children = _children;
- }
- };
- */
- class Solution {
- public:
- vector<int> postorder(Node* root) {
- stack<Node*> Stack;
- vector<int> Postorder;
- if(root==nullptr)
- {
- return Postorder;
- }
- Stack.push(root);
- while(!Stack.empty())
- {
- Node* temp = Stack.top();
- Stack.pop();
- Postorder.push_back(temp->val);
- for (int i = temp->children.size() - 1; i >= 0; i--)
- {
- Stack.push(temp->children[(temp->children.size() - 1)-i]);
- }
- }
- reverse(Postorder.begin(),Postorder.end());
- return Postorder;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment