码迷,mamicode.com
首页 > 其他好文 > 详细

1043. Is It a Binary Search Tree (25)

时间:2015-12-06 11:20:07      阅读:371      评论:0      收藏:0      [点我收藏+]

标签:

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

(二叉树建立方法)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node‘s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node‘s key.
  • Both the left and right subtrees must also be binary search trees.

If we swap the left and right subtrees of every node, then the resulting tree is called the Mirror Image of a BST.

Now given a sequence of integer keys, you are supposed to tell if it is the preorder traversal sequence of a BST or the mirror image of a BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (<=1000). Then N integer keys are given in the next line. All the numbers in a line are separated by a space.

Output Specification:

For each test case, first print in a line "YES" if the sequence is the preorder traversal sequence of a BST or the mirror image of a BST, or "NO" if not. Then if the answer is "YES", print in the next line the postorder traversal sequence of that tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input 1:
7
8 6 5 7 10 8 11
Sample Output 1:
YES
5 7 6 8 11 10 8
Sample Input 2:
7
8 10 11 8 6 7 5
Sample Output 2:
YES
11 8 10 7 5 6 8
Sample Input 3:
7
8 6 8 5 10 9 11
Sample Output 3:
NO
 

  1. #include<iostream>
  2. using namespace std;
  3. struct BST {
  4. int val;
  5. BST* left;
  6. BST* right;
  7. };
  8. int a[1005], flag = 1;
  9. BST* BuildBST(int s,int e) {
  10. int i, j;
  11. if (s > e)
  12. return NULL;
  13. for ( i = s + 1; i <= e; i++) {
  14. if (a[s] <= a[i]) {
  15. break;
  16. }
  17. }
  18. for ( j = i; j <= e; j++) {
  19. if (a[s] > a[j]) {
  20. flag = 0;
  21. return NULL;
  22. }
  23. }
  24. BST *node=(BST*)malloc(sizeof(BST));
  25. node->val = a[s];
  26. node->left = BuildBST(s + 1, i - 1);
  27. node->right = BuildBST(i, e);
  28. return node;
  29. }
  30. BST* BuildMirrorBST(int s, int e) {
  31. int i, j;
  32. if (s > e)
  33. return NULL;
  34. for ( i = s + 1; i <= e; i++) {
  35. if (a[s] > a[i]) {
  36. break;
  37. }
  38. }
  39. for ( j = i; j <= e; j++) {
  40. if (a[s] <= a[j]) {
  41. flag = 0;
  42. return NULL;
  43. }
  44. }
  45. BST *node = (BST*)malloc(sizeof(BST));
  46. node->val = a[s];
  47. node->left = BuildMirrorBST(s + 1, i - 1);
  48. node->right = BuildMirrorBST(i, e);
  49. return node;
  50. }
  51. void PrintPostorder(BST* node,int cnt) {
  52. if (node == NULL) {
  53. return;
  54. }
  55. PrintPostorder(node->left,cnt+1);
  56. PrintPostorder(node->right,cnt+1);
  57. if (cnt == 0)
  58. cout << node->val;
  59. else
  60. cout << node->val<< " " ;
  61. }
  62. int main(void) {
  63. int n;
  64. cin >> n;
  65. for (int i = 0; i < n; i++) {
  66. cin >> a[i];
  67. }
  68. BST* root = BuildBST(0, n - 1);
  69. if (flag) {
  70. cout << "YES" << endl;
  71. PrintPostorder(root,0);
  72. }
  73. else {
  74. flag = 1;
  75. BST* root = BuildMirrorBST(0, n - 1);
  76. if (flag) {
  77. cout << "YES" << endl;
  78. PrintPostorder(root,0);
  79. }
  80. else {
  81. cout << "NO" << endl;
  82. }
  83. }
  84. return 0;
  85. }





1043. Is It a Binary Search Tree (25)

标签:

原文地址:http://www.cnblogs.com/zzandliz/p/5023152.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!