Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)

1.后序遍历的非递归实现。(左右根)

难点:后序遍历的非递归实现是三种遍历方式中最难的一种。因为在后序遍历中,要保证左孩子和右孩子都已被访问并且左孩子在右孩子前访问才能访问根结点,这就为流程的控制带来了难题。下面介绍两种思路。

思路:有个关键的就是unUsed这个标识符。

当unUsed=1时,表示该节点未遍历过,即以该节点为根节点的左右孩子不曾遍历。

当unUsed=0时,表示该节点的左右孩子都已经被访问过。

由于入栈的顺序和出栈的顺序相反,所以若unUsed=1,则左根右节点依次入栈,且根节点的unUsed置为0.

代码:

class Solution {
public:
    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> res;
        if(root==NULL) return res;
        stack<pair<TreeNode*,int>> s;
        int unUsed;
        s.push(make_pair(root,1));

        while (!s.empty())
        {
            root=s.top().first;
            unUsed=s.top().second;
            s.pop();
            if(unUsed){
                s.push(make_pair(root,0));
                if(root->right)
                    s.push(make_pair(root->right,1));
                if(root->left)
                    s.push(make_pair(root->left,1));
            }else{
                //cout<<root->val<<" ";
                res.push_back(root->val);
            }
        }
    }
};

2.拓展,中序遍历的非递归实现(左根右)。和以上思路类似,由于也不能先访问根节点,但是我们是从根节点出发,所以还是使用unUsed来标志根节点的状态。

往栈中放节点的顺序倒过来即:左根右,且该根节点的左右孩子访问完毕。

代码:

void inOrder(Node *p)
{
     if(!p)
         return;
     stack< pair<Node*,int> > s;
     Node *t;
     int unUsed;
     s.push(make_pair(p,1));
     while(!s.empty())
     {
         t=s.top().first;
         unUsed = s.top().second;
         s.pop();
         if(unUsed)
         {
               if(t->right)
                     s.push( make_pair(t->right,1) );
              s.push( make_pair(t,0) );
              if(t->left)
                     s.push( make_pair(t->left,1));
          }
          else printf("%d\n",t->data);
       }
}

3.前序非递归,直接先根节点。

void preOrder(Node *p) //非递归
{
   if(!p) return;
   stack<Node*> s;
   Node *t;
   s.push(p);
   while(!s.empty())
   {
       t=s.top();
       printf("%d\n",t->data);
       s.pop();
       if(t->right) s.push(t->right);
       if(t->left) s.push(t->left);
    }
 }
时间: 2024-08-26 17:07:09

Binary Tree Postorder Traversal(各种非递归实现,完美利用栈结构模拟)的相关文章

[LeetCode] Binary Tree Preorder Traversal (非递归的先序遍历)

Binary Tree Preorder Traversal Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively 解题思路: 二叉树的前序遍历.

Binary Tree Inorder Traversal [leetcode] 非递归的三种解法

第一种方法是Morris Traversal 是O(n)时间复杂度,且不需要额外空间的方法.缺点是需要修改树. 通过将叶子节点的right指向其中序后继. 代码如下 vector<int> inorderTraversal(TreeNode *root) { vector<int> res; TreeNode * cur = root; TreeNode * pre = NULL; while (cur) { if (cur->left == NULL) { res.push

94.Binary Tree Inorder Traversal(非递归中序遍历)

Given a binary tree, return the inorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,3,2]. Note: Recursive solution istrivial, could you do it iteratively? confused what "{1,#,2,3}" means? > rea

Binary Tree Preorder Traversal (非递归实现)

具体思路参见:二叉树的非递归遍历(转) /** * 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> preorderTraversal(TreeNode *

94. Binary Tree Inorder Traversal(非递归实现二叉树的中序遍历)

Given a binary tree, return the inorder traversal of its nodes' values. Example: Input: [1,null,2,3] 1 2 / 3 Output: [1,3,2] Follow up: Recursive solution is trivial, could you do it iteratively? 方法一:递归 /** * Definition for a binary tree node. * publ

144.Binary Tree Preorder Traversal(非递归前序遍历)

Given a binary tree, return the preorder traversal of itsnodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [1,2,3]. Note: Recursive solution istrivial, could you do it iteratively? HideTags Tree Stack #pragma once #include<ios

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) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

leetcode - Binary Tree Preorder Traversal &amp;&amp; Binary Tree Inorder Traversal &amp;&amp; Binary Tree Postorder Traversal

简单来说,就是二叉树的前序.中序.后序遍历,包括了递归和非递归的方法 前序遍历(注释中的为递归版本): 1 #include <vector> 2 #include <stack> 3 #include <stddef.h> 4 #include <iostream> 5 6 using namespace std; 7 8 struct TreeNode 9 { 10 int val; 11 TreeNode *left; 12 TreeNode *rig

LeetCode: Binary Tree Postorder Traversal [145]

[题目] 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? [题意] 非递归实现后续遍历 [思路] 维护两个栈,一个栈用来存储标记,标记相