标签:后序遍历 stream return 输出 info image bsp creat ace
大致题意就是给出一个二叉树的先序、中序遍历序列,要求输出二叉树后序遍历的第一个元素。
1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 50010; 5 int n,pre[maxn],in[maxn],flag = 1; 6 7 void create(int preL,int preR,int inL,int inR) { 8 if(inL > inR ||!flag) return; 9 int k,leftNum; 10 for(k = inL; k<= inR; ++k) 11 if(in[k] == pre[preL]) break; 12 leftNum = k-inL; 13 create(preL+1,preL+leftNum,inL,k-1); 14 create(preL+leftNum+1,preR,k+1,inR); 15 if(flag) printf("%d",pre[preL]),flag = 0; 16 } 17 18 int main() { 19 cin>>n; 20 for(int i = 0; i < n; ++i) 21 scanf("%d",&pre[i]); 22 for(int i = 0; i < n; ++i) 23 scanf("%d",&in[i]); 24 create(0,n-1,0,n-1); 25 return 0; 26 }
1138 Postorder Traversal(先序+中序=二叉树)
标签:后序遍历 stream return 输出 info image bsp creat ace
原文地址:https://www.cnblogs.com/keep23456/p/12496808.html