Binary Tree Postorder Traversal Solutions in C++
Number 145
Difficulty Hard
Acceptance 55.1%
Link LeetCode
Solutions
C++ solution by haoel/leetcode
// Source : https://oj.leetcode.com/problems/binary-tree-postorder-traversal/// Author : Hao Chen// Date : 2014-07-21#include <stdio.h>#include <stdlib.h>#include <time.h>#include <iostream>#include <vector>#include <algorithm>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};vector<int> postorderTraversal1(TreeNode *root);vector<int> postorderTraversal2(TreeNode *root);// We have two methods here.// 1) the first one acutally is pre-order but reversed the access order.// 2) the second one is the traditional onevector<int> postorderTraversal(TreeNode *root) {if (random()%2){cout << "---method one---" << endl;return postorderTraversal1(root);}cout << "---method two---" << endl;return postorderTraversal2(root);}vector<int> postorderTraversal1(TreeNode *root) {vector<int> v;vector<TreeNode*> stack;if (root) {stack.push_back(root);}while (stack.size()>0){TreeNode *n = stack.back();stack.pop_back();v.push_back(n->val);if (n->left){stack.push_back(n->left);}if (n->right) {stack.push_back(n->right);}}std::reverse(v.begin(), v.end()); // the trickreturn v;}// traditional and standard way.// using the stack to simulate the recursive function stack.vector<int> postorderTraversal2(TreeNode *root) {vector<int> v;vector<TreeNode*> stack;TreeNode *node = root;TreeNode *lastVisitNode = NULL;while(stack.size()>0 || node!=NULL){if (node != NULL){// keep going the leftstack.push_back(node);node = node->left;}else{TreeNode *n = stack.back();// left way is finsised, keep going to the right wayif (n->right != NULL && lastVisitNode != n->right){node = n->right;}else{// both left and right has been accessed.stack.pop_back();v.push_back(n->val);lastVisitNode = n;}}}return v;}TreeNode* createTree(int a[], int n){if (n<=0) return NULL;TreeNode **tree = new TreeNode*[n];for(int i=0; i<n; i++) {if (a[i]==0 ){tree[i] = NULL;continue;}tree[i] = new TreeNode(a[i]);}int pos=1;for(int i=0; i<n && pos<n; i++) {if (tree[i]){tree[i]->left = tree[pos++];if (pos<n){tree[i]->right = tree[pos++];}}}return tree[0];}void printTree_post_order(TreeNode *root){if (root == NULL){//cout << "# ";return ;}printTree_post_order(root->left);printTree_post_order(root->right);cout << root->val << " ";}void printArray(vector<int> v){for(int i=0; i<v.size(); i++){cout << v[i] << " ";}cout << endl;}int main(){srand(time(0));int a[] = {1,2,3,4,5,0,6,0,0,7,8,9,0};TreeNode* p = createTree(a, sizeof(a)/sizeof(int));printTree_post_order(p);cout << endl;vector<int> v = postorderTraversal(p);printArray(v);cout << endl;return 0;}
C++ solution by pezy/LeetCode
#include <vector>#include <stack>#include <cstddef>using std::vector; using std::stack;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:vector<int> postorderTraversal(TreeNode *root) {vector<int> retv;TreeNode *last = NULL;for (stack<TreeNode*> stk; root || !stk.empty(); ){if (root){stk.push(root);root = root->left;}else if (stk.top()->right && stk.top()->right != last){root = stk.top()->right;}else{last = stk.top();retv.push_back(last->val); stk.pop();}}return retv;}};
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Recursive// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;__postorderTraversal(root, res);return res;}private:void __postorderTraversal(TreeNode* node, vector<int> &res){if( node ){__postorderTraversal(node->left, res);__postorderTraversal(node->right, res);res.push_back(node->val);}}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2017-11-17#include <iostream>#include <vector>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// My Non-Recursive// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {private:struct Command{string s; // go, printTreeNode* node;Command(string s, TreeNode* node): s(s), node(node){}};public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<Command> stack;stack.push(Command("go", root) );while(!stack.empty()){Command command = stack.top();stack.pop();if(command.s == "print")res.push_back(command.node->val);else{assert(command.s == "go");stack.push(Command("print", command.node));if(command.node->right)stack.push(Command("go",command.node->right));if(command.node->left)stack.push(Command("go",command.node->left));}}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Non-Recursive// Using a tag to record whether the node has been visited//// Time Complexity: O(n), n is the node number in the tree// Space Complexity: O(h), h is the height of the treeclass Solution {private:struct TagNode{TreeNode* node;bool isFirst;TagNode(TreeNode* node): node(node), isFirst(false){}};public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TagNode> stack;TreeNode* cur = root;while(cur != NULL || !stack.empty()){while(cur != NULL){stack.push(TagNode(cur));cur = cur->left;}TagNode tagNode = stack.top();stack.pop();cur = tagNode.node;if(tagNode.isFirst == false){tagNode.isFirst = true;stack.push(tagNode);cur = cur->right;}else{res.push_back(cur->val);cur = NULL;};}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Non-Recursive// Using two stacks, Reverse the Preorder Traversal!//// Time Complexity: O(n)// Space Complexity: O(n)class Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack, output;stack.push(root);while(!stack.empty()){TreeNode* node = stack.top();stack.pop();output.push(node);if(node->left != NULL)stack.push(node->left);if(node->right != NULL)stack.push(node->right);}while(!output.empty()){res.push_back((output.top())->val);output.pop();}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-30#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Non-Recursive// Using two stacks, Reverse the Preorder Traversal!//// Time Complexity: O(n)// Space Complexity: O(n)class Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack, output;TreeNode* p = root;while(p != NULL || !stack.empty()){if(p != NULL){stack.push(p);output.push(p);p = p->right;}else{p = stack.top();stack.pop();p = p->left;}}while(!output.empty()){res.push_back((output.top())->val);output.pop();}return res;}};int main() {return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Non-Recursive// Using a pre pointer to record the last visted node//// Time Complexity: O(n)// Space Complexity: O(h)class Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack;TreeNode* pre = NULL;stack.push(root);while(!stack.empty()){TreeNode* node = stack.top();stack.pop();if((node->left == NULL && node->right == NULL) ||(pre != NULL && pre == node->left && node->right == NULL) ||(pre != NULL && pre == node->right)){res.push_back(node->val);pre = node;}else{stack.push(node);if(node->right != NULL)stack.push(node->right);if(node->left != NULL)stack.push(node->left);}}return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {TreeNode* root = new TreeNode(1);root->right = new TreeNode(2);root->right->left = new TreeNode(3);print_vec(Solution().postorderTraversal(root));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Classic Non-Recursive// Using a pre pointer to record the last visted node//// Time Complexity: O(n)// Space Complexity: O(h)class Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack;TreeNode* pre = NULL;TreeNode* cur = root;while(cur != NULL || !stack.empty()){while(cur != NULL){stack.push(cur);cur = cur->left;}cur = stack.top();stack.pop();if(cur->right == NULL || pre == cur->right){res.push_back(cur->val);pre = cur;cur = NULL;}else{stack.push(cur);cur = cur->right;}}return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {TreeNode* root = new TreeNode(1);root->right = new TreeNode(2);root->right->left = new TreeNode(3);print_vec(Solution().postorderTraversal(root));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Classic Non-Recursive// Using a pre pointer to record the last visted node//// Time Complexity: O(n)// Space Complexity: O(h)class Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;stack<TreeNode*> stack;TreeNode* pre = NULL;TreeNode* cur = root;while(cur != NULL || !stack.empty()){if(cur != NULL){stack.push(cur);cur = cur->left;}else{cur = stack.top();stack.pop();if(cur->right == NULL || pre == cur->right){res.push_back(cur->val);pre = cur;cur = NULL;}else{stack.push(cur);cur = cur->right;}}}return res;}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {TreeNode* root = new TreeNode(1);root->right = new TreeNode(2);root->right->left = new TreeNode(3);print_vec(Solution().postorderTraversal(root));return 0;}
C++ solution by liuyubobobo/Play-Leetcode
/// Source : https://leetcode.com/problems/binary-tree-postorder-traversal/description//// Author : liuyubobobo/// Time : 2018-05-31#include <iostream>#include <vector>#include <stack>using namespace std;/// Definition for a binary tree node.struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};// Morris PostOrder Traversal//// Time Complexity: O(n)// Space Complexity: O(1)class Solution {public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;if(root == NULL)return res;TreeNode* dummyRoot = new TreeNode(-1);dummyRoot->left = root;TreeNode* cur = dummyRoot;while(cur != NULL){if(cur->left == NULL)cur = cur->right;else{TreeNode* prev = cur->left;while(prev->right != NULL && prev->right != cur)prev = prev->right;if(prev->right == NULL){prev->right = cur;cur = cur->left;}else{prev->right = NULL;reverseTraversal(cur->left, res);cur = cur->right;}}}delete dummyRoot;return res;}private:void reverseTraversal(TreeNode* node, vector<int>& res){int start = res.size();while(node != NULL){res.push_back(node->val);node = node->right;}reverse(res.begin() + start, res.end());}};void print_vec(const vector<int>& vec){for(int e: vec)cout << e << " ";cout << endl;}int main() {TreeNode* root = new TreeNode(1);root->right = new TreeNode(2);root->right->left = new TreeNode(3);print_vec(Solution().postorderTraversal(root));return 0;}