TOJ 2801. Binary Trees

给出一棵树的后序遍历,小写字母表示叶子节点,每个中间节点都有两个叶子节点,求中序遍历。

由于给出的是后序遍历,故最后一个节点是根节点,且对于每个子树而言,令叶子节点数为n,度为2的节点数为n2,(在这个题目里面所有的节点都有两个叶子节点,故所有非叶子节点的个数为n2),有这样的性质:n=n2+1.

故除却根节点来说,子树中的小写字母一定比大写字母多1,递归即可。

Maybe you have taken the course called Data Structure before. You should be familiar with the fact that no parenthesis is needed in the suffix expression. But have you ever wondered why?

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

  • The post-order traversal of a binary tree is to visit the left subtree of the root first, then the right subtree and finally the root.
  • The in-order traversal of a binary tree is to visit the left subtree of the root first, then the root, and finally the right subtree.
  • Constraints

  • S contains letters('a'-'z', 'A'-'Z') only and don't contain any white spaces.
  • The length of S is between 3 and 999, inclusive.
  • The length of S is an odd number.
  • S will be the post-order traversal of a binary tree.
  • Sample Input

    2
    bcA
    efBghCA
    

    Sample Output

    bAc
    eBfAgCh
    
    #include<iostream>
    #include<string>
    using namespace std;
    string s;
    int solv(int top,int end){
    	if(top==end){
    		cout<<s[top];
    		return 0;
    	}
    	else{
    		int t,count=0;
    		for(t=end-1;t>=top;t--){
    			if(s[t]>='a'&&s[t]<='z')
    				count++;
    			else
    				count--;
    			if(count==1)
    				break;
    		}
    		solv(top,t-1);
    		cout<<s[end];
    		solv(t,end-1);
    	}
    }
    int main(){
    	int n;
    	//string s;
    	cin>>n;
    	while(n--){
    		cin>>s;
    		solv(0,s.length()-1);
    		cout<<endl;
    	}
    	return 0;
    }


    • 0
      点赞
    • 0
      收藏
      觉得还不错? 一键收藏
    • 0
      评论

    “相关推荐”对你有帮助么?

    • 非常没帮助
    • 没帮助
    • 一般
    • 有帮助
    • 非常有帮助
    提交
    评论
    添加红包

    请填写红包祝福语或标题

    红包个数最小为10个

    红包金额最低5元

    当前余额3.43前往充值 >
    需支付:10.00
    成就一亿技术人!
    领取后你会自动成为博主和红包主的粉丝 规则
    hope_wisdom
    发出的红包
    实付
    使用余额支付
    点击重新获取
    扫码支付
    钱包余额 0

    抵扣说明:

    1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
    2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

    余额充值