/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ /*后序非递归遍历算法de思想 由于镜像二叉树的先序遍历=原二叉树后序遍历, 可以先求出镜像先序,最后reverse一下即可。 */ class Solution { public: vector<int> postorderTraversal(TreeNode* root) { stack <TreeNode*> stk; vector<int> out; TreeNode* tmp; stk.push(root); if(!root) return out; while(!stk.empty()){ tmp=stk.top(); out.push_back(tmp->val); stk.pop(); if(tmp->left) stk.push(tmp->left); if(tmp->right) stk.push(tmp->right); } reverse(out.begin(),out.end()); return out; } };