1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| class Solution { public:
vector<int> postorderTraversal(TreeNode* root) { vector<int> res; stack<pair<TreeNode *, int>> s; s.push({root, 0}); while (!s.empty()) { pair<TreeNode *, int> node = s.top(); s.pop(); if(node.first == nullptr) { continue; } if(!node.second) { s.push({node.first, 1}); s.push({node.first->right, 0}); s.push({node.first->left, 0}); } else { res.push_back(node.first->val); } } return res; } };
|