hdu 3999 BST
来源:互联网 发布:淘宝在哪儿买彩票 编辑:程序博客网 时间:2024/05/16 01:11
题意:给你一个整数序列,按照这个序列构造一颗二叉排序树
输出一个整数序列,要求按照这个序列构造的二叉排序树的形状和原来的一样,而且序列的字典序最小
做法:按照题目的序列构造好二叉排序树后直接按照先根遍历的顺序输出即可,因为左儿子小于右儿子,所以在根先输出的情况下,尽量都先输出左儿子,刚好符合先根遍历的特点
从来不写指针,最近也偶尔写写,尝试不同的方法
View Code
#include<stdio.h>#include<string.h>#include<stdlib.h>struct node{ int num; struct node *left; struct node *right;}*tree;int tot=0;node *build(node *root,int num){ if(root==NULL) { root=new node; root->left=NULL; root->right=NULL; root->num=num; return root; } else { int t=root->num-num;; if(t>0) root->left=build(root->left,num); else root->right=build(root->right,num); return root; }}bool first;void visit(node *root){ if(root!=NULL&&!first) printf(" %d",root->num); else { first=false; printf("%d",root->num); }}void preorder(node *root){ if(root!=NULL) { visit(root); preorder(root->left); preorder(root->right); }}int main(){ int n,num; while(scanf("%d",&n)!=EOF) { first=true; int num; for(int i=1;i<=n;i++) { scanf("%d",&num); tree=build(tree,num); } preorder(tree); puts(""); } return 0;}
- hdu 3999 BST
- HDU 3999 BST基础
- HDU 3999 BST + 先序遍历
- HDU 3791 BST 基础
- HDU 3791 BST
- HDU 3999 The order of a Tree 二叉搜索树 BST
- BST
- BST
- BST
- BST
- BST
- BST
- bst
- BST
- BST
- BST
- BST
- BST
- poj 2892 树状数组 二分
- poj 3204 Ikki's Story I - Road Reconstruction 网络流
- poj 3422 Kaka's Matrix Travels hdu 3376 matrix again 费用流
- poj 2976 分数规划
- 贴贴水题
- hdu 3999 BST
- hdu 3397 Sequence operation 线段树
- poj 3334 计算几何
- codeforces 180D
- poj 1986 RMQ&&LCA( 模板题)
- codeforces 182D Common Divisors KMP做法
- codeforces 182E Wooden Fence 动态规划
- codeforces 115E Linear Kingdom Races 线段树 + DP 好题
- 后缀数组论文推荐题