hdu 3999 The order of a Tree (二叉搜索树)

 1 /******************************************************************
 2 题目:        The order of a Tree(hdu 3999)
 3 链接:        http://acm.hdu.edu.cn/showproblem.php?pid=3999
 4 题意:        给你一个序列建立一棵二叉搜索树 要你找出另外一个序
 5               列,可以建立和原序列建立的二叉搜索树一样且这个序列
 6               是字典序最小
 7 算法:        二叉搜索树
 8 思想:        对于一个二叉搜索树,它的先序遍历是能建立该二叉搜索
 9               树字典序最小序列
10 ******************************************************************/
11 #include<cstdio>
12 #include<cstring>
13 #include<cstdlib>
14 #include<iostream>
15 using namespace std;
16
17 typedef struct Tree
18 {
19     Tree *left,*right;
20     int num;
21 }Tree;
22 Tree *t;
23
24 Tree *inser(Tree *p,int x)
25 {
26     if (p==NULL)
27     {
28         p=(Tree *) malloc(sizeof(Tree));
29         p->left=p->right=NULL;
30         p->num=x;
31         return p;
32     }
33     if (p->num>x)
34     {
35         p->left=inser(p->left,x);
36         return p;
37     }
38     else
39     {
40         p->right=inser(p->right,x);
41         return p;
42     }
43 }
44
45 void Find(Tree *p,int flag)
46 {
47     if (p==NULL) return ;
48     if (flag) printf("%d",p->num);
49     else printf(" %d",p->num);
50     Find(p->left,0);
51     Find(p->right,0);
52     delete(p);
53 }
54
55 int main()
56 {
57     int n;
58     while (~scanf("%d",&n))
59     {
60         t=NULL;
61         for (int i=0;i<n;i++)
62         {
63             int a;
64             scanf("%d",&a);
65             t=inser(t,a);
66         }
67         Find(t,1);
68         printf("\n");
69     }
70 }
时间: 2024-09-28 06:13:39

hdu 3999 The order of a Tree (二叉搜索树)的相关文章

hdu 3999 The order of a Tree

题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3999 The order of a Tree Description As 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 beco

538. Convert BST to Greater Tree 二叉搜索树转换为更大树

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST. Example: Input: The root of a Binary Search Tree like thi

235. Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的LCA

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点

Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has

[LeetCode]501. Find Mode in Binary Search Tree二叉搜索树寻找众数

这次是二叉搜索树的遍历 感觉只要和二叉搜索树的题目,都要用到一个重要性质: 中序遍历二叉搜索树的结果是一个递增序列: 而且要注意,在递归遍历树的时候,有些参数如果是要随递归不断更新(也就是如果递归返回上层,参数也需要最新的),就要用全局变量,而不是传参,其实这就是全局变量的定义. 不过如果是保存每层递归内的信息,就需要传参数. 本题显然是需要全局参数 /** * Definition for a binary tree node. * public class TreeNode { * int

HDU 3999 The order of a Tree 二叉树

#include <iostream> #include <cstdlib> #include <string.h> #include <algorithm> #define ss(a) scanf("%d",&a) #define ss64(a) scanf("%I64d",&a) using namespace std; typedef struct Node { struct Node *l; s

1099 Build A Binary Search Tree [二叉搜索树/中序、层次遍历]

先用中序确定节点的值,再用层次遍历输出即可. 写的时候思维江化,一开始用指针建树... #include <bits/stdc++.h> using namespace std; #define maxn 105 struct Node { int index,left,right; }node[maxn]; int n,a[maxn],tree[maxn]; vector<int> ve[maxn],in,post; void inorder(int p) { if(node[p

LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一个排序好的数组转换成二叉搜索树,这题没想出来,基本上是参考别人的,边界条件应该注意一下: 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * T

HDU 3791 二叉搜索树 题解

Problem Description 判断两序列是否为同一二叉搜索树序列 Input 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接下去一行是一个序列,序列长度小于10,包含(0~9)的数字,没有重复数字,根据这个序列可以构造出一颗二叉搜索树. 接下去的n行有n个序列,每个序列格式跟第一个序列一样,请判断这两个序列是否能组成同一颗二叉搜索树. Output 如果序列相同则输出YES,否则输出NO Sample Input 2 567432 543267