画图出来后结果很明显

2
xyPzwIM
abcABdefgCDEF sample output
wzyxIPM
gfCecbDdAaEBF *
+ -
x y z w F
B E
a A d D
b c e C
f g

#include<cstdio>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std; struct node
{
node(node* l, node* r, char ch): left(l), right(r), c(ch) {}
node* left;
node* right;
char c;
}*root; void build_tree(string & str)
{
stack<node*> st;
for(int i=0;i<str.length();i++)
{
if(islower(str[i]))
{
st.push(new node(0, 0, str[i]));
}
else
{
node* right=st.top(); st.pop();
node* left=st.top(); st.pop();
st.push(new node(left, right, str[i]));
}
} root=st.top();
} vector<char> ans; void bfs()
{
ans.clear();
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* nd=q.front();q.pop();
ans.push_back(nd->c);
if(nd->left)
{
q.push(nd->left);
} if(nd->right)
{
q.push(nd->right);
}
}
} void delete_tree(node* nd)
{
if(nd)
{
delete_tree(nd->left);
delete_tree(nd->right);
delete nd;
}
} void output()
{
for(int i=ans.size()-1;i>=0;i--)
cout<<ans[i];
cout<<endl;
} int main()
{
int n;
cin>>n;
string str;
while(n--)
{
cin>>str;
build_tree(str);
bfs();
delete_tree(root);
output();
} return 0;
}

UVa 11234 Expressions (二叉树重建&由叶往根的层次遍历)的更多相关文章

  1. uva 11234 Expressions 表达式 建树+BFS层次遍历

    题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include < ...

  2. UVA 548(二叉树重建与遍历)

    J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Ap ...

  3. lintcode : 二叉树的层次遍历

    题目 二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历 ...

  4. [LintCode] Binary Tree Level Order Traversal(二叉树的层次遍历)

    描述 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 3 / \ 9 20 / \ 15 7 返回他的分层遍历结果: [ [3] ...

  5. 二叉树的层次遍历 · Binary Tree Level Order Traversal

    [抄题]: 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) [思维问题]: [一句话思路]: 用queue存每一层 [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况 ...

  6. 毕业了C++二叉树层次遍历

    //代码经过测试,赋值粘贴即可用#include<iostream> #include<stdio.h> #include<stack> #include<q ...

  7. LintCode-69.二叉树的层次遍历

    二叉树的层次遍历 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 样例 给一棵二叉树 {3,9,20,#,#,15,7} : 返回他的分层遍历结果: [     [3],     [9,2 ...

  8. lintcode_69_二叉树的层次遍历

    二叉树的层次遍历   描述 笔记 数据 评测 给出一棵二叉树,返回其节点值的层次遍历(逐层从左往右访问) 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? LinkedIn Airb ...

  9. 【遍历二叉树】06二叉树曲折(Z字形)层次遍历II【Binary Tree Zigzag Level Order Traversal】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的Z字形层次 ...

随机推荐

  1. 【C#学习笔记】写文件

    using System; using System.IO; namespace ConsoleApplication { class Program { static void Main(strin ...

  2. LeetCode: Interval

    (1)Merge Intervals https://leetcode.com/problems/merge-intervals/ Given a collection of intervals, m ...

  3. java运用FFMPEG视频转码技术

    基于windows系统安装FFMPEG转码技术 http://wenku.baidu.com/link?url=z4Tv3CUXxxzLpa5QPI-FmfFtrIQeiCYNq6Uhe6QCHkU- ...

  4. javascript实现map的功能(转载)

    /* * MAP对象,实现MAP功能 * * 接口: * size() 获取MAP元素个数 * isEmpty() 判断MAP是否为空 * clear() 删除MAP所有元素 * put(key, v ...

  5. T-SQL查询进阶-10分钟理解游标

    转:http://www.cnblogs.com/CareySon/archive/2011/11/01/2231381.html 概述 游标是邪恶的! 在关系数据库中,我们对于查询的思考是面向集合的 ...

  6. K2 blackpearl 安装

    转:http://blog.csdn.net/gxiangzi/article/details/8432188 K2是国外的一款BPM引擎,基于MS的Workflow,关于它的详细介绍在我之前一片博客 ...

  7. CXF之jaxws:endpoint对spring bean的引用

    由于CXF对spring的无缝支持,CXF的使用,经常与spring捆绑在一起.随之而起的,自然是想在jaxws:endpoint中引用spring bean.在CXF提供的HelloWorld例子中 ...

  8. C++ STL算法系列3---求和:accumulate

    该算法在numeric头文件中定义. 假设vec是一个int型的vector对象,下面的代码: //sum the elements in vec starting the summation wit ...

  9. Linux--使用expect进行自动交互

    在linux下进行一些操作时,有时需要与机器进行一些交互操作,比如切换账号时输入账号密码,传输文件时输入账号密码登陆远程机器等,但有时候这些动作需要在shell脚本中进行,这个时候就可以使用expec ...

  10. Android JNI之调用JAVA方法的返回类型签名

    从http://blog.csdn.net/lonelyroamer/article/details/7932787截取的 如何签名: 下面看看Sign签名如何写,来表示要取得的属性或方法的类型. 1 ...