The order of a Tree HDU
来源:互联网 发布:sql增加数据 编辑:程序博客网 时间:2024/05/28 18:42
The order of a Tree
HDU - 3999As we know,the shape of a binary search tree is greatly related to the order of keys we insert. To be precisely:
1. insert a key k to a empty tree, then the tree become a tree with
only one node;
2. insert a key k to a nonempty tree, if k is less than the root ,insert
it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.
1. insert a key k to a empty tree, then the tree become a tree with
only one node;
2. insert a key k to a nonempty tree, if k is less than the root ,insert
it to the left sub-tree;else insert k to the right sub-tree.
We call the order of keys we insert “the order of a tree”,your task is,given a oder of a tree, find the order of a tree with the least lexicographic order that generate the same tree.Two trees are the same if and only if they have the same shape.
41 3 4 2
1 3 2 4
代码:
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;typedef struct nodetree{ struct nodetree *l,*r;//左右孩子节点 int key;//父亲节点} tree;tree *root;int b[110000],a[110000];int c,n;tree *creat(int x)//通过申请空间建立临时指针指向父亲节点{ //动态申请一个空间,用来存放一个节点,并用临时指针t指向这个节点 tree *t=(tree *)malloc(sizeof(tree)); t->l=NULL;//把当前节点的左右孩子节点为空 t->r=NULL; t->key=x; return t;}tree *insert(tree *s,int x){ tree *t; if(s==NULL) { t=creat(x);//调用creat函数创建父亲节点 s=t; //printf("*****\n"); } else//递归调用自己创建左右子树 { if(x>s->key)//按照左小右大规则建立二叉树 { s->r=insert(s->r,x); } else s->l=insert(s->l,x); } return s;}void find(tree *root)//先序遍历{ if(root!=NULL) { if(c<n) { printf("%d ",root->key); c++; } else printf("%d\n",root->key); find(root->l);//先序遍历左子树 find(root->r);//先序遍历右子树 }}int main(){ while(~scanf("%d",&n)) { int str[110000]; int len,i; root=NULL; for(i=0;i<n;i++) scanf("%d",&str[i]); for(i=0; i<n; i++) { int num=str[i]; root=insert(root,num);//调用insert函数把各个节点插入树中 //root的值改变 } c=1; find(root); //printf("\n"); }}
阅读全文
0 0
- hdu 3999The order of a Tree
- Hdu 3999 - The order of a Tree
- HDU 3999 The order of a Tree
- The order of a Tree HDU
- hdu 3999:The order of a Tree
- HDU The order of a Tree
- hdu 3999The order of a Tree
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree
- HDU 3999 The order of a Tree
- The order of a Tree HDU
- hdu 3999 The order of a Tree
- hdu 3999 The order of a Tree
- HDU-3999 The order of a Tree 树的遍历
- HDU 3999 The order of a Tree 二叉树
- HDU3999:The order of a Tree
- iOS UINavigationController
- 模拟 co.js 的实现原理 (koa中间件的实现)
- 翻译QT基于TCP聊天室server
- gulp+jsp初探
- [数据库与SQL]
- The order of a Tree HDU
- 路径压缩 递归 与非递归版本
- 007 UseGCLogFileRotation NumberOfGCLogFiles GCLogFileSize
- C#中bin与obj
- AIDL使用入门, 客户端通过AIDL与服务端通信
- Xib隐藏控件以及frame(位置)改变
- 1806:词典( 3.9数据结构之C++STL)
- 初识nodejs12
- Spark2.2内核剖析