145. Binary Tree Postorder Traversal

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].

解题思路:

方法一:递归方法。。。


class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
postorder(root,res);
return res;
}
void postorder(TreeNode* root,vector<int>&res){
if(root==NULL)return ;
if(root->left!=NULL)postorder(root->left,res);
if(root->right!=NULL)postorder(root->right,res);
res.push_back(root->val);
}
};

方法二:非递归方法

参考: http://zhidao.baidu.com/link?url=hi29YFNWekIU9L4YJV0hqzuzB6YXtQdH-7BJvfUUvzB731gxdH1zMKfAZkBUo4JlhupBobBuqusGqy-nrmJ9Fc79KE6i0WJ7tvb6sk3

每次将节点的左结点放入栈中,对于最左节点,若存在右节点,则将其放入栈中,修改最左节点的右节点为空,同时将该右节点的左节点依次放入栈中。若找到一个最左节点,没有右节点,则该节点被访问。。。


// iterator
// reference:http://zhidao.baidu.com/link?url=hi29Y_FNWekIU9L4YJV0hqzuzB6YXtQdH-7BJvfUUvzB731gxdH1_zMKfAZkBUo4JlhupBobBuq_usGqy-nrm_J9Fc79KE6i0WJ7tvb6sk3
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
if(root==NULL)return res;
stack<TreeNode*>s;
TreeNode*p=root;
while(p){
s.push(p);
p=p->left;
while(!s.empty()){
p=s.top();
//cout<<p->val<<endl;
if(p->right){
//cout<<p->right->val<<endl;
s.push(p->right);
TreeNode*q=p->right;
p->right=NULL;
p=q;
while(p->left){
}else{
s.pop();
res.push_back(p->val);
}
return res;
}

};

方法三:类似前序遍历,不过将访问顺序改为根结点,右节点,左节点的顺序,栈可以实现。该过程是后序遍历的逆序。


// another iterator
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
vector<int>res;
if(root==NULL)return res;
stack<TreeNode*>s;
s.push(root);
while(!s.empty()){
TreeNode*p=s.top();
s.pop();
res.push_back(p->val);
if(p->left)s.push(p->left);
if(p->right)s.push(p->right);
}
std::reverse(res.begin(),res.end());
return res;
}

};

时间: 2024-10-02 04:31:35

145. Binary Tree Postorder Traversal的相关文章

&lt;LeetCode OJ&gt; 145. Binary Tree Postorder Traversal

在此之前回顾前序遍历和中序遍历: 1,前序遍历: 基本规则,总是先访问根节点在左节点,在右节点 递归解法: class Solution { public: vector<int> result; vector<int> preorderTraversal(TreeNode* root) { if(root){ result.push_back(root->val); preorderTraversal(root->left); preorderTraversal(ro

【leetcode】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio

【LeetCode】145. Binary Tree Postorder Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51494797 Subject 出处:https://leetcode.com/problems/binary-tree-postorder-traversal/ Hard 级别 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree

LeetCode解题报告:Binary Tree Postorder Traversal

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? 注意:下面是迭代的解法.理解有点困难,和大家讨论一下. 1 import java.uti

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recur

LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)

翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 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: Recur

Java for LeetCode 145 Binary Tree Postorder Traversal

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]. 后序遍历,左子树→右子树→根节点 前序遍历的非递归实现需要一个计数器,方法是需要重写一个类继承TreeNode,翁慧玉教材<数据结构:题解与拓展>P113有详细介绍,这里略.递归JAVA实现如下: public Lis

145. Binary Tree Postorder Traversal (Stack, Tree)

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? class Solution { public: vector<int> postor

145. Binary Tree Postorder Traversal QuestionEditorial Solution

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? 每次都先push right, 然后left. 但是需要一个sentinel存每次上一次p