Summary: Basic staff, need to check if goes up along the tree, that's the difference to pre-order traverse.
1 vector<int> postorderTraversal(TreeNode *root) { 2 vector<int> seq; 3 if(root == NULL) 4 return seq; 5 stack<TreeNode *> nodes; 6 nodes.push(root); 7 TreeNode * pre_node = NULL; 8 while(nodes.size() != 0) { 9 TreeNode * node = nodes.top(); 10 if(node->left == NULL && node->right == NULL || 11 (pre_node != NULL && (pre_node == node->left || pre_node == node->right))){ 12 pre_node = node; 13 seq.push_back(node->val); 14 nodes.pop(); 15 continue; 16 } 17 18 if(node->right != NULL) { 19 nodes.push(node->right); 20 } 21 22 if(node->left != NULL) 23 nodes.push(node->left); 24 } 25 }