码迷,mamicode.com
首页 > 其他好文 > 详细

Tree Traversals

时间:2015-09-16 01:07:01      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:tree traversals

非递归中序遍历

  1. Push 的顺序为先序遍历

  2. Pop 的顺序给出中序遍历


Sample Input:   

技术分享

6

Push 1

Push 2

Push 3

Pop

Pop

Push 4

Pop

Pop

Push 5

Push 6

Pop

Pop


技术分享

void solve( int preL, int inL, int postL, int n )

    { if (n==0) return;

    if (n==1) {post[postL] = pre[preL]; return;}

    root = pre[preL];

    post[postL+n-1] = root;

        for (i=0; i<n; i++)

            if (in[inL+i] == root) break;

            L = i; R = n-L-1;

    solve(preL+1, inL, postL, L);

    solve(preL+L+1, inL+L+1, postL+L, R);

}


Tree Traversals

标签:tree traversals

原文地址:http://zhenzhuangde.blog.51cto.com/10697385/1695099

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!