题解 | #二叉树遍历#
二叉树遍历
https://www.nowcoder.com/practice/6e732a9632bc4d12b442469aed7fe9ce
#include<iostream>
#include<stack>
using namespace std;
void out(string s1,string s2,int low1,int high1,int low2,int high2,stack<char> &s){
if(low1 > high1 || low2 > high2)return;
char head = s1.at(low1);
int p = low2;
while(s2.at(p)!=head)p++;
s.push(head);
int length1 = p-low2;//1 2 3 4(p) 5 6 7
//4(p) 2 3 1 5 7 6
int length2 = high2-p;
out(s1,s2,low1+1,low1+length1,p-length1,p-1,s);
out(s1,s2,low1+length1+1,high1,p+1,high2,s);
cout<<head;
s.pop();
}
int main(){
string s1 = "";
string s2 = "";
while(cin>>s1){
if(cin.eof())break;
cin>>s2;
stack <char>s;
out(s1,s2,0,s1.length()-1,0,s2.length()-1,s);
cout<<endl;
}
return 0;
}
传音控股晋升空间 51人发布

查看11道真题和解析