English translat

We can number binary trees using the following scheme:
The empty tree is numbered 0.
The single-node tree is numbered 1.
All binary trees having m nodes have numbers less than all those having m+1 nodes.
Any binary tree having m nodes with left and right subtrees L and R is numbered n such that all trees having m nodes numbered > n have either Left subtrees numbered higher than L, or A left subtree = L and a right subtree numbered higher than R.

The first 10 binary trees and tree number 20 in this sequence are shown below:


Your job for this problem is to output a binary tree when given its order number.

输入

Input consists of multiple problem instances. Each instance consists of a single integer n, where 1 <= n <= 500,000,000. A value of n = 0 terminates input. (Note that this means you will never have to output the empty tree.)

输出

For each problem instance, you should output one line containing the tree corresponding to the order number for that instance. To print out the tree, use the following scheme:

A tree with no children should be output as X.
A tree with left and right subtrees L and R should be output as (L')X(R'), where L' and R' are the representations of L and R.
If L is empty, just output X(R').
If R is empty, just output (L')X.

我们可以使用以下方案对二叉树进行编号:
空树编号为 0。
单节点树编号为 1。
所有具有 m 个节点的二叉树的数量都小于所有具有 m+1 个节点的二叉树。
任何具有 m 个节点且具有左右子树 L 和 R 的二叉树都被编号为 n,这样所有具有 m 个节点编号 > n 的树都具有编号高于 L 的左子树,或者一个左子树 = L 和一个编号高于 R 的右子树.

该序列中的前 10 棵二叉树和第 20 棵树如下所示:

这个问题你的工作是在给定序号时输出一个二叉树。

输入

输入由多个问题实例组成。每个实例由一个整数 n 组成,其中 1 <= n <= 500,000,000。 n = 0 的值终止输入。 (请注意,这意味着您永远不必输出空树。)

输出

对于每个问题实例,您应该输出一行,其中包含与该实例的订单号对应的树。要打印出树,请使用以下方案:

一棵没有孩子的树应该输出为 X。
具有左右子树 L 和 R 的树应输出为 (L')X(R'),其中 L' 和 R' 是 L 和 R 的表示。
如果 L 为空,则输出 X(R')。
如果 R 为空,则输出 (L')X。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值