Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> postorderTraversal(TreeNode* root) {
- vector<int> ans;
- while(root!=nullptr){
- ans.push_back(root->val);
- if(root->right == nullptr){
- root = root->left;
- }
- else{
- TreeNode* next = root->right;
- while(next->left!=nullptr){
- next = next->left;
- }
- next->left = root->left;
- TreeNode* temp = root;
- root = root->right;
- temp->left=nullptr;
- }
- }
- reverse(ans.begin(), ans.end());
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment