大厂笔试题
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
3.9 KiB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. struct BstNode
  5. {
  6. int data;
  7. struct BstNode *left;
  8. struct BstNode *right;
  9. };
  10. struct BstNode *GetNewNode(int data)
  11. {
  12. struct BstNode *temp = (struct BstNode *)malloc(sizeof(struct BstNode));
  13. temp->data = data;
  14. temp->left = NULL;
  15. temp->right = NULL;
  16. return temp;
  17. }
  18. // 在二叉搜索树中插入一个节点
  19. struct BstNode *Insert(struct BstNode *root, int data)
  20. {
  21. if (root == NULL)
  22. {
  23. root = GetNewNode(data);
  24. }
  25. else if (data <= root->data)
  26. {
  27. root->left = Insert(root->left, data);
  28. }
  29. else
  30. {
  31. root->right = Insert(root->right, data);
  32. }
  33. return root;
  34. }
  35. // 在二叉搜索树中搜索一个节点
  36. bool Search(struct BstNode *root, int data)
  37. {
  38. if (root == NULL)
  39. return false;
  40. else if (root->data == data)
  41. return true;
  42. else if (data <= root->data)
  43. return Search(root->left, data);
  44. else
  45. return Search(root->right, data);
  46. }
  47. // 二叉搜索树的最小值
  48. int FindMin(struct BstNode *root)
  49. {
  50. struct BstNode *temp = root;
  51. if (temp == NULL)
  52. {
  53. printf("root is empty\n");
  54. return -1;
  55. }
  56. else
  57. {
  58. while (temp->left != NULL)
  59. {
  60. temp = temp->left;
  61. }
  62. return temp->data;
  63. }
  64. }
  65. // 二叉搜索树的最大值
  66. int FindMax(struct BstNode *root)
  67. {
  68. struct BstNode *temp = root;
  69. if (temp == NULL)
  70. {
  71. printf("root is empty\n");
  72. return -1;
  73. }
  74. else
  75. {
  76. while (temp->right != NULL)
  77. {
  78. temp = temp->right;
  79. }
  80. return temp->data;
  81. }
  82. }
  83. // 二叉搜索树的前序遍历
  84. void Preorder(struct BstNode *root)
  85. {
  86. if (root == NULL)
  87. return;
  88. printf("%d,", root->data);
  89. Preorder(root->left);
  90. Preorder(root->right);
  91. }
  92. // 二叉搜索树的中序遍历
  93. void Inorder(struct BstNode *root)
  94. {
  95. if (root == NULL)
  96. return;
  97. Inorder(root->left);
  98. printf("%d,", root->data);
  99. Inorder(root->right);
  100. }
  101. // 二叉搜索树的后序遍历
  102. void Poseorder(struct BstNode *root)
  103. {
  104. if (root == NULL)
  105. return;
  106. Poseorder(root->left);
  107. Poseorder(root->right);
  108. printf("%d,", root->data);
  109. }
  110. // 判断是否是二叉搜索树
  111. bool IsSubtreeLesser(struct BstNode *root, int data)
  112. {
  113. if (root == NULL)
  114. return true;
  115. if (root->data <= data && IsSubtreeLesser(root->left, data) && IsSubtreeLesser(root->right, data))
  116. return true;
  117. else
  118. return false;
  119. }
  120. bool IsSubtreeGreater(struct BstNode *root, int data)
  121. {
  122. if (root == NULL)
  123. return true;
  124. if (root->data >= data && IsSubtreeGreater(root->left, data) && IsSubtreeGreater(root->right, data))
  125. return true;
  126. else
  127. return false;
  128. }
  129. bool IsBinarySearchTree(struct BstNode *root)
  130. {
  131. if (root == NULL)
  132. return true;
  133. if (IsSubtreeLesser(root->left, root->data) && IsSubtreeGreater(root->right, root->data) && IsBinarySearchTree(root->left) && IsBinarySearchTree(root->right))
  134. return true;
  135. else
  136. return false;
  137. }
  138. // 二叉搜索树的高度
  139. int FindHeight(struct BstNode *root)
  140. {
  141. int LeftHeight = 0;
  142. int RightHeight = 0;
  143. if (root == NULL)
  144. {
  145. return -1;
  146. }
  147. LeftHeight = FindHeight(root->left);
  148. RightHeight = FindHeight(root->right);
  149. if (LeftHeight >= RightHeight)
  150. return LeftHeight + 1;
  151. else
  152. return RightHeight + 1;
  153. }
  154. int main()
  155. {
  156. struct BstNode *root = NULL;
  157. printf("input start!\n");
  158. root = Insert(root, 10);
  159. root = Insert(root, 8);
  160. root = Insert(root, 7);
  161. root = Insert(root, 10);
  162. root = Insert(root, 35);
  163. root = Insert(root, 50);
  164. printf("input yes!\n");
  165. printf("%d", FindHeight(root));
  166. // Inorder(root);
  167. // if(IsBinarySearchTree(root) == true)printf("This is IsBinarySearchTree");
  168. // else printf("This is not IsBinarySearchTree");
  169. // if(Search(root,30) == true)printf("Found!\n");
  170. // else printf("Not Found!\n");
  171. // printf("%d\n",FindMax(root));
  172. // printf("%d",FindMin(root));
  173. }