[UVA 10701]Pre,in and post (二叉树)(dfs)
来源:互联网 发布:淘宝客自动采集api 编辑:程序博客网 时间:2024/06/05 20:51
题目大意:
得到中左右,左中右,求左右中。
思路:
缓冲一下用dfs求出中右左并用stack储存就可以。
真情实感的写代码都是会得到好运的。
瞧,和 UVA 536 Tree Recovery 一毛一样。
我写的好像跟常规不太像要再研究下别人的。
/*uva 10706 Pre,in and post (dfs) by zhuhuaTime limit: 3000msAC time: 0ms???*/#include <iostream>#include <stack>using namespace std;string precord,incord;stack <char> ReOut;void dfs(string a,string b){ if(a=="")return; char head=a[0]; ReOut.push(head); int pos=b.find(head,0); //cout<<head<<endl; string lefta,leftb,righta,rightb; righta=a.substr(pos+1,a.length()-pos-1); rightb=b.substr(pos+1,b.length()-pos-1); //cout<<righta<<" "<<rightb<<endl; dfs(righta,rightb); lefta=a.substr(1,pos); leftb=b.substr(0,pos); dfs(lefta,leftb);}int main(){int t,len;cin>>t; while(cin>>len>>precord>>incord) //pre:root,left,right //in:left,root,right { dfs(precord,incord); while(!ReOut.empty()) { cout<<ReOut.top(); ReOut.pop(); }cout<<endl; } return 0;}
阅读全文
0 0
- [UVA 10701]Pre,in and post (二叉树)(dfs)
- UVa, 10701 Pre, in and post
- UVa 10701 - Pre, in and post
- UVa10701 - Pre, in and post
- 1119. Pre- and Post-order Traversals (30) 二叉树
- 1119. Pre- and Post-order Traversals (30)[二叉树遍历]
- Compile, Pre, and Post Linking in AngularJS
- 非递归方法实现 二叉树的 DFS(Pre Order), Post Order
- In sum for pre- / In- and Post- order Traversal
- 二叉树pre-order,post-order,in-order,level-order遍历实现,及衍生问题
- 1119. Pre- and Post-order Traversals (30) (先序+后序,确定二叉树?)
- Pre,in,post expression
- UVA 112(二叉树、DFS)
- UVA 548(DFS)+二叉树遍历
- Pre- and Post-order Traversals
- 用DFS计算pre和post
- [LeetCode] Construct/build binary tree from in-order and post-order/pre-order traversal
- UVa 548 - Tree【二叉树的递归遍历(DFS)】
- 基于java的Spring容器配置(二)(@Configuration,@Bean,@Import,@ImportSource)
- PAT乙级1003
- 用proteus来看二极管的压降
- Flash中的XML
- [UVA
- [UVA 10701]Pre,in and post (二叉树)(dfs)
- 颠倒的价牌
- 解决 IDEA 中src下xml等资源文件无法读取的问题
- 循环双链表的基本操作实现
- 面向对象(类、封装、this、构造方法)
- html 返回顶部
- 结合redis设计与实现的redis源码学习-6-intset(整数集合)
- [6.837]A0:迭代函数系统(IFS)
- 变态跳台阶(剑指Offer 第 9 题)