http://www.cnblogs.com/keam37/p/3637717.html  keam所有 转载请注明出处

Problem Description

We can number binary trees using the following scheme:

我们用下面的规则来给一颗二叉树编号:
The empty tree is numbered 0.

空树编号为0。
The single-node tree is numbered 1.

只有一个节点的树编号为1。
All binary trees having m nodes have numbers less than all those having m+1 nodes.

所有m个节点的树的编号不会大于有m+1个节点的树的编号。
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.

任意一个编号为n,有m个节点,左右子树节点分别为L和R的二叉树。当一棵树的编号>n,不是该树的左子树的编号大于L,就是左子树编号相同,右子树编号大于R;
The first 10 binary trees and tree number 20 in this sequence are shown below:

前10颗二叉树和第20颗树如下所示:

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

你的任务是依据给定编号,输出一颗树.

Input

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.)

Output

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.

Sample Input

1

20

31117532 0

Sample Output

X

((X)X(X))X

(X(X(((X(X))X(X))X(X))))X(((X((X)X((X)X)))X)X)

 

分析:

要打印这颗树,首先要确定树的节点数。

先用递推计算n个节点的树有多少颗,就能得到节点小于k的树一共有Sum[k],这样对于输入的编号n

总的第n棵树,就是有k个节点的第(n-sum[k-1])棵树.

再确定左右子树分别是总的第几棵树,再递归即可

参考代码

#include <iostream>
using namespace std;
//定义0个节点时有1种二叉树,用于递推
int radix[20] = {1};
int sRadix[20];
//递归求k个节点的第n颗树
int make (int n, int k)
{
//只有一个节点时直接输出'X'
if (k == 1) {
cout << 'X';
return 0;
}
//nL,nR代表左右子树的节点个数,sL,sR代表左右子树分别是当前节点数的第几颗树
int nL = 0, sL = 0, nR = k - 1, sR = n;
//如果sR>nR个节点最多二叉树的种数,左子树序号+1,p帮助累计左子树节点
for (int p = 0; radix[nR] < sR; sL++) {
sR -= radix[nR];
if (p) p--;
//如果p==0,左子树节点数+1,右节点数-1,更新p为nL个节点的左子树的种数-1
else {
nL++, nR--;
p = radix[nL] - 1;
}
}
//递归子树时为子树加上括号
if (nL > 0) {
cout << '('; make (sL - sRadix[nL - 1], nL);
cout << ')';
}
cout << 'X';
if (nR > 0) {
cout << '('; make (sR, nR);
cout << ')';
}
return 0;
}
int main()
{
int n, k;
//递推,求出radix[i]。代表i个节点的二叉树有几种
for (int i = 1; i <= 18; i++) {
for (int j = 0; j <= (i - 1) / 2; j++)
radix[i] += radix[j] * radix[i - j - 1] * 2;
if (~ (i - 1) & 1) radix[i] -= radix[ (i - 1) / 2] * radix[ (i - 1) / 2];
//sRadix数组方便求出第N颗数有几个节点
sRadix[i] = sRadix[i - 1] + radix[i];
}
while (cin >> n && n) {
for (k = 1; sRadix[k] < n; k++);
make (n - sRadix[k - 1], k);
cout<<endl;
}
return 0;
}

HDU.P1100 Trees Made to Order 解题报告的更多相关文章

  1. 【LeetCode】1030. Matrix Cells in Distance Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  2. 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...

  3. hdu 1002.A + B Problem II 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1002 题目意思:就是大整数加法. 两年几前做的,纯粹是整理下来的. #include <stdi ...

  4. hdu 1004 Let the Balloon Rise 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 用STL 中的 Map 写的 #include <iostream> #includ ...

  5. hdu 2066 一个人的旅行 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2066 题目意思:给出T条路,和草儿家相邻的城市编号,以及草儿想去的地方的编号.问从草儿家到达草儿想去的 ...

  6. hdu 2680 Choose the best route 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2680 题目意思:实质就是给定一个多源点到单一终点的最短路. 卑鄙题---有向图.初始化map时 千万不 ...

  7. HDU 1010 temp of the bone 解题报告 (DFS)

    转载大佬的blog,很详细,学到了很多东西 奇偶剪枝:根据题目,dog必须在第t秒到达门口.也就是需要走t-1步.设dog开始的位置为(sx,sy),目标位置为(ex,ey).如果abs(ex-x)+ ...

  8. 【LeetCode】1114. Print in Order 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 mutex锁 promise/future 日期 题 ...

  9. poj 2013 Symmetric Order 解题报告

    题目链接:http://poj.org/problem?id=2013 设长度非递减的字串序列为s[1]...s[n].设计递归子程序print(n),其中n为字串序号,每分析1个字串,n=n-1. ...

随机推荐

  1. listBox 搜索左右移动

    <td align="left" width="50%"> 查询:<asp:TextBox ID="SacffSearch" ...

  2. Boost库编译安装

    一.Boost库介绍         Boost库是一个经过千锤百炼.可移植.提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一.Boost库由C++标准委员会库工作组成员发起,其 ...

  3. 【C++】异常简述(一):C语言中的异常处理机制

    人的一生会遇到很多大起大落,尤其是程序员. 程序员写好的程序,论其消亡形式无非三种:无疾而终.自杀.他杀. 当然作为一名程序员,最乐意看到自己写的程序能够无疾而终,因此尽快的学习异常处理机制是非常重要 ...

  4. pthread Win32多线程编程的一些知识和感想

    研究遗传算法的一大诟病就是每次运行程序的结果并不是完全一样的,有时候能找到最优解有时候找不到最优解,这就是遗传算法的概率性导致的.那么怎么评价你的方法的好坏呐,这时候就要多次独立运行程序最后取结果的平 ...

  5. Vue全局添加组件或者模块

    import Api from './api.js' export default { install (Vue) { Vue.prototype.$Api = Api } } 这种格式就可以在mai ...

  6. CAS机制总结

    一.简介 CAS机制:(Compare and set)比较和替换 简单来说–>使用一个期望值来和当前变量的值进行比较,如果当前的变量值与我们期望的值相等,就用一个新的值来更新当前变量的值CAS ...

  7. eclipse如何设置多个字符的智能提示

    clipse代码里面的代码提示功能默认是关闭的,只有输入“.”的时候才会提示功能,用vs的用户可能不太习惯这种,vs是输入任何字母都会提示,下面说一下如何修改eclipse配置,开启代码自动提示功能打 ...

  8. 2.C# 输入一个整数,求质因数

    C# 输入一个整数,求质因数 List<int> results = new List<int>(); int number = Int32.Parse(Console.Rea ...

  9. JFinal项目eclipse出现Unknown column 'createtime' in 'order clause' 的错误

    JFinal项目eclipse出现Unknown column 'createtime' in 'order clause' 的错误,在本次项目中的原因是我的表中的字段信息中创建时间的字段是creat ...

  10. python3+beautifulSoup4.6抓取某网站小说(一)爬虫初探

    本次学习重点: 1.使用urllib的request进行网页请求,获取当前url整版网页内容 2.对于多级抓取,先想好抓取思路,再动手 3.BeautifulSoup获取html网页中的指定内容 4. ...