二叉树的前中后序遍历非递归实现

1、前序遍历非递归实现

 1 class Solution {
 2 public:
 3     /*
 4      * @param root: A Tree
 5      * @return: Preorder in ArrayList which contains node values.
 6      */
 7     vector<int> preorderTraversal(TreeNode * root) {
 8         vector<int> res;
 9         if(root==NULL)return res;
10         std::stack<TreeNode *> sta;
11         sta.push(root);
12         while(sta.size())
13         {
14             TreeNode *p=sta.top();
15             sta.pop();
16             res.push_back(p->val);
17             if(p->right)sta.push(p->right);
18             if(p->left)sta.push(p->left);
19         }
20         return res;
21     }
22 };

2、中序遍历非递归实现

 1 class Solution {
 2 public:
 3     /*
 4      * @param root: A Tree
 5      * @return: Inorder in ArrayList which contains node values.
 6      */
 7     vector<int> inorderTraversal(TreeNode * root) {
 8        vector<int> res;
 9        if(root==NULL)return res;
10        std::stack<TreeNode *> sta;
11        TreeNode *p=root;
12        while(p || sta.size())
13        {
14            if(p){
15                sta.push(p);
16                p=p->left;
17            }else{
18                p=sta.top();
19                sta.pop();
20                res.push_back(p->val);
21                p=p->right;
22            }
23        }
24        return res;
25     }
26 };

3、后序遍历非递归实现

 1 class Solution {
 2 public:
 3     /*
 4      * @param root: A Tree
 5      * @return: Postorder in ArrayList which contains node values.
 6      */
 7     vector<int> postorderTraversal(TreeNode * root) {
 8         vector<int> res;
 9         if(root==NULL)return res;
10         std::stack<TreeNode *> sta;
11         std::stack<int> help;//增加一个辅助栈
12         sta.push(root);
13         while(sta.size())
14         {
15             TreeNode *p=sta.top();
16             sta.pop();
17             help.push(p->val);//先放入辅助栈
18             if(p->left)sta.push(p->left);
19             if(p->right)sta.push(p->right);
20         }
21         while(help.size())//再从辅助栈内倒出
22         {
23             int tmp=help.top();
24             help.pop();
25             res.push_back(tmp);
26         }
27         return res;
28     }
29 };

 

转载于:https://www.cnblogs.com/jeysin/p/8422083.html

1.先序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void PreOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { visite(p->data); push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) //通过下一次循环的内嵌while实现右子树遍历 { p=pop(s); p=p->rchild; }//endif }//endwhile }//PreOrderUnrec2.序遍历非递归算法#define maxsize 100typedef struct{ Bitree Elem[maxsize]; int top;}SqStack;void InOrderUnrec(Bitree t){ SqStack s; StackInit(s); p=t; while (p!=null || !StackEmpty(s)) { while (p!=null) //遍历左子树 { push(s,p); p=p->lchild; }//endwhile if (!StackEmpty(s)) { p=pop(s); visite(p->data); //访问根结点 p=p->rchild; //通过下一次循环实现右子树遍历 }//endif }//endwhile}//InOrderUnrec3.后序遍历非递归算法#define maxsize 100typedef enum{L,R} tagtype;typedef struct { Bitree ptr; tagtype tag;}stacknode;typedef struct{ stacknode Elem[maxsize]; int top;}SqStack;void PostOrderUnrec(Bitree t){ SqStack s; stacknode x; StackInit(s); p=t; do { while (p!=null) //遍历左子树 { x.ptr = p; x.tag = L; //标记为左子树 push(s,x); p=p->lchild; } while (!StackEmpty(s) && s.Elem[s.top].tag==R) { x = pop(s); p = x.ptr; visite(p->data); //tag为R,表示右子树访问完毕,故访问根结点 } if (!StackEmpty(s)) { s.Elem[s.top].tag =R; //遍历右子树 p=s.Elem[s.top].ptr->rchild; } }while (!StackEmpty(s));}//PostOrderUnrec
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值