TOJ 2801.Binary Trees(二叉树后序转中序)
来源:互联网 发布:布吕歇尔 知乎 编辑:程序博客网 时间:2024/06/06 01:04
题目链接:http://acm.tju.edu.cn/toj/showp2801.html
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 2195 Accepted Runs: 804
We know that an expression can be uniquely represented by a binary tree. The suffix expression is the post-order traversal of this tree. Given the post-order traversal of a binary tree, each of whose non-leaf nodes has exactly two children, we can reconstruct the original binary tree if we are told which nodes in the post-order traversal are non-leaves. This means that no parenthesis is needed in the suffix expression.
Here is your task. Given a string S which represents the post-order traversal of a binary tree T in which all of the non-leaf nodes have exactly two children, and given which nodes in S are non-leaves, you must reconstruct the binary tree and output the in-order traversal of T.
Each node of T has a label, which is a letter('a'-'z', 'A'-'Z'). A lowercase letter('a'-'z') means the corresponding node is a leaf and an uppercase letter('A'-'Z') means the corresponding node is a non-leaf.
Input
The input contains an integer on the first line, which indicates the number of test cases. Each test case contains one string S on a line, the post-order traversal of a binary tree.Output
For each test case, output on a line one string which is the in-order traversal of the corresponding binary tree. There can be no white spaces in your output.Notes
Constraints
Sample Input
2bcAefBghCA
Sample Output
bAceBfAgCh
Source: The 5th UESTC Programming Contest
二叉树后序转中序,由于本题有限定条件为非叶子节点都有两个子节点,注意二叉树性质:n0=n2+1;然后就可以写了:
#include <stdio.h>#include <iostream>using namespace std;string a;void binTree(int s,int e){if(s==e){cout<<a[s];return ;}int length=0,j;for(j=e-1;j>=s;j--){if(a[j]>='a'&&a[j]<='z')length++;elselength--;if(length==1)break;}binTree(s,j-1);cout<<a[e];binTree(j,e-1);}int main(){int cast;scanf("%d",&cast);while(cast--){cin>>a;binTree(0,a.length()-1);cout<<endl;}}
- TOJ 2801.Binary Trees(二叉树后序转中序)
- TOJ 2801. Binary Trees
- 二叉搜索树(Binary Search Trees)
- Glib学习(9) 平衡二叉树 Balanced Binary Trees
- LeetCode 96. Unique Binary Search Trees(唯一二叉树)
- LeetCode:Unique Binary Search Trees(二叉查找树)
- 二叉搜索树(Binary Search Trees)小结
- 617. Merge Two Binary Trees(二叉树的合并)
- 617. Merge Two Binary Trees(合并二叉树)
- 【二叉树】617. Merge Two Binary Trees
- Unique Binary Search Trees--独特的二叉树个数(重)
- LeetCode | Unique Binary Search Trees(二叉搜索树的个数)
- LeetCode | Unique Binary Search Trees II(构建二叉搜索树)
- LeetCode OJ 之 Unique Binary Search Trees (不同的二叉搜索树)
- LeetCode OJ 之 Unique Binary Search Trees II (不同的二叉搜索树 - 二)
- leetCode 95.Unique Binary Search Trees II (唯一二叉搜索树) 解题思路和方法
- 【LeetCode-面试算法经典-Java实现】【096-Unique Binary Search Trees(唯一二叉搜索树)】
- LeetCode 95. Unique Binary Search Trees II(唯一二叉搜索树)
- php 使用cURL 模拟注册
- jni 入门实例
- 多线程基础
- Java基础复习
- 拨号键盘
- TOJ 2801.Binary Trees(二叉树后序转中序)
- 《产品前线:48位一线互联网产品经理的智慧与实战》读书笔记4
- Gallery与ImageSwitcher总结
- Android中一键退出程序方法
- 【codeforces711B】Bus to Udayland
- 数据库分库分表(sharding)系列(二) 全局主键生成策略
- 短信聊天
- C++重要术语解释
- 深入理解计算机系统(读书笔记)