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

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
/*
1)如果根节点非空,将根节点加入到栈中。
2)如果栈不空,取栈顶元素(暂时不弹出),
   如果(左子树已访问过或者左子树为空),且(右子树已访问过或右子树为空),则弹出栈顶节点,将其值加入数组,
   如果左子树不为空,且未访问过,则将左子节点加入栈中,并标左子树已访问过。
   如果右子树不为空,且未访问过,则将右子节点加入栈中,并标右子树已访问过。
3)重复第二步,直到栈空。*/
struct stkNode
{
    TreeNode *node;
    bool lVisited;
    bool rVisited;
    stkNode(TreeNode *p){node = p; lVisited= false; rVisited= false;}
};
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        stack<stkNode*> stk;
        vector<int> result;
        if(root==NULL) return result;
        stk.push( new stkNode(root));
        while(!stk.empty())
        {
            stkNode *stknode = stk.top();
            if((!stknode->node->left || stknode->lVisited)&&(!stknode->node->right || stknode->rVisited))
            {
               stk.pop();
               result.push_back(stknode->node->val);
               delete stknode;
            }else if(stknode->node->left && !stknode->lVisited)
            {
               stk.push(new stkNode(stknode->node->left));
               stknode->lVisited = true;
            }else if(stknode->node->right && !stknode->rVisited)
            {
                stk.push(new stkNode(stknode->node->right));
               stknode->rVisited = true;
            }
        }
        return result;
    }
};
时间: 2024-10-28 23:23:04

145. Binary Tree Postorder Traversal非递归,栈实现的相关文章

【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 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

【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——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? Show Tags #include<iostream> #include<vec

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>

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

[leedcode 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]. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right

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