Binary Tree Postorder Traversal @LeetCode
PROBLEM :
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Recursive Solution)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* 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> vec ;
if(root==NULL)
return vec ;
postorder(root,vec) ;
return vec ;
}
void postorder(TreeNode* root,vector<int> &vec){
if(root==NULL)
return ;
postorder(root->left,vec) ;
postorder(root->right,vec) ;
vec.push_back(root->val) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Recursive Solution - Using Two Stacks)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* 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> vec ;
if(root==NULL)
return vec ;
stack<TreeNode*> sta1 ;
stack<TreeNode*> sta2 ;
sta1.push(root) ;
while(!sta1.empty()){
TreeNode* curr=sta1.top() ;
sta1.pop() ;
sta2.push(curr) ;
if(curr->left)
sta1.push(curr->left) ;
if(curr->right)
sta1.push(curr->right) ;
}
while(!sta2.empty()){
vec.push_back((sta2.top())->val) ;
sta2.pop() ;
}
return vec ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Recursive Solution - Using One Stack)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* 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> vec ;
if(root==NULL)
return vec ;
stack<TreeNode*> sta ;
sta.push(root) ;
while(!sta.empty()){
TreeNode* curr=sta.top() ;
sta.pop() ;
vec.push_back(curr->val) ;
if(curr->left)
sta.push(curr->left) ;
if(curr->right)
sta.push(curr->right) ;
}
reverse(vec.begin(),vec.end());
return vec ;
}
};
---------------------------------------------------------------------------------
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [3,2,1].
Note: Recursive solution is trivial, could you do it iteratively?
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Recursive Solution)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* 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> vec ;
if(root==NULL)
return vec ;
postorder(root,vec) ;
return vec ;
}
void postorder(TreeNode* root,vector<int> &vec){
if(root==NULL)
return ;
postorder(root->left,vec) ;
postorder(root->right,vec) ;
vec.push_back(root->val) ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Recursive Solution - Using Two Stacks)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* 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> vec ;
if(root==NULL)
return vec ;
stack<TreeNode*> sta1 ;
stack<TreeNode*> sta2 ;
sta1.push(root) ;
while(!sta1.empty()){
TreeNode* curr=sta1.top() ;
sta1.pop() ;
sta2.push(curr) ;
if(curr->left)
sta1.push(curr->left) ;
if(curr->right)
sta1.push(curr->right) ;
}
while(!sta2.empty()){
vec.push_back((sta2.top())->val) ;
sta2.pop() ;
}
return vec ;
}
};
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION : (Recursive Solution - Using One Stack)
--------------------------------------------------------------------------------
/**
* Definition for a binary tree node.
* 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> vec ;
if(root==NULL)
return vec ;
stack<TreeNode*> sta ;
sta.push(root) ;
while(!sta.empty()){
TreeNode* curr=sta.top() ;
sta.pop() ;
vec.push_back(curr->val) ;
if(curr->left)
sta.push(curr->left) ;
if(curr->right)
sta.push(curr->right) ;
}
reverse(vec.begin(),vec.end());
return vec ;
}
};
---------------------------------------------------------------------------------
Comments
Post a Comment