题解 | #二叉树遍历#

二叉树遍历

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

全部评论

相关推荐

点赞 评论 收藏
分享
认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
06-20 21:22
已编辑
门头沟学院 Java
纯真的河老师在喝茶:答应了就跑啊,实习随便跑啊,别被pua了,md就是找个廉价劳动力,还平稳过度正式工,到时候跟你说没转正
点赞 评论 收藏
分享
今天投了小鹏,收到了AI面,大概会问哪些啊?
期末一定及格:总共4个部分,心理测评、行测、然后就是问岗位、对岗位的理解、过往遇到了哪些难点怎么解决,很简单,没有什么特别专业的问题,都是一些综合素质相关的
小鹏汽车AI面6人在聊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务