非递归中序遍历
Push 的顺序为先序遍历
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);
}
原文地址:http://zhenzhuangde.blog.51cto.com/10697385/1695099