首页 > 其他 > 详细

Tree Traversals

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

非递归中序遍历

  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

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!