题解 | #二叉树遍历#

二叉树遍历

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;
}

全部评论

相关推荐

11-13 20:32
门头沟学院 Java
面向未来编程code:我没看到他咋急,他不就问你个问题。。。
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务