LEETCODE: Binary Tree Postorder Traversal
2015-01-10 14:08
204 查看
Given a binary tree, return the postorder traversal of its nodes' values.
For example:
Given binary tree
return
Note: Recursive solution is trivial, could you do it iteratively?
/**
* Definition for binary tree
* 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> result;
stack<pair<TreeNode*, int>> noderecords;
stack<int> nodeStatus;
if(root != NULL){
noderecords.push(make_pair(root, 0));
}
while(!noderecords.empty()){
TreeNode* n = noderecords.top().first;
int status = noderecords.top().second;
noderecords.pop();
if(status == 0){
noderecords.push(make_pair(n, 1));
if(n->right != NULL){
noderecords.push(make_pair(n->right, 0));
}
if(n->left != NULL){
noderecords.push(make_pair(n->left, 0));
}
}
else{
result.push_back(n->val);
}
}
return result;
}
};
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?
/**
* Definition for binary tree
* 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> result;
stack<pair<TreeNode*, int>> noderecords;
stack<int> nodeStatus;
if(root != NULL){
noderecords.push(make_pair(root, 0));
}
while(!noderecords.empty()){
TreeNode* n = noderecords.top().first;
int status = noderecords.top().second;
noderecords.pop();
if(status == 0){
noderecords.push(make_pair(n, 1));
if(n->right != NULL){
noderecords.push(make_pair(n->right, 0));
}
if(n->left != NULL){
noderecords.push(make_pair(n->left, 0));
}
}
else{
result.push_back(n->val);
}
}
return result;
}
};
相关文章推荐
- LeetCode --- Binary Tree Postorder Traversal
- [LeetCode][Java] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode: Construct Binary Tree from Inorder and Postorder Traversal [106]
- leetcode -day23 Construct Binary Tree from Inorder and Postorder Traversal & Construct Binary Tree f
- [Leetcode 106, Medium] Construct Binary Tree from Inorder and Postorder Traversal
- Leetcode 之Binary Tree Postorder Traversal(46)
- LeetCode No.106 Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode(145)Binary Tree Postorder Traversal
- 【Leetcode】Construct Binary Tree from Inorder and Postorder Traversal
- leetcode:二叉树之Construct Binary Tree from Inorder and Postorder Traversal
- leetcode Construct Binary Tree from Inorder and Postorder Traversal 难度系数3 3.38
- 【LeetCode】Binary Tree Postorder Traversal
- Leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode145—Binary Tree Postorder Traversal
- LeetCode Construct Binary Tree from Inorder and Postorder Traversal
- [LeetCode] 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树
- LeetCode 105/106 Construct Binary Tree from Preorder/Postorder and Inorder Traversal
- Binary Tree Postorder Traversal—LeetCode
- [LeetCode] Construct Binary Tree from Inorder and Postorder Traversal
- LeetCode 145 Binary Tree Postorder Traversal (后序遍历二叉树)