My LeetCode grinding. Trying to do a problem a day.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

main.c 956B

123456789101112131415161718192021222324252627282930313233343536373839
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. struct TreeNode {
  4. int val;
  5. struct TreeNode *left;
  6. struct TreeNode *right;
  7. };
  8. struct TreeNode* searchBST(struct TreeNode*, int);
  9. struct TreeNode* searchBST(struct TreeNode* root, int val){
  10. if (root == NULL) {
  11. return NULL;
  12. } else if (root->val == val) {
  13. return root;
  14. } else if (root->val > val) {
  15. return searchBST(root->left, val);
  16. } else {
  17. return searchBST(root->right, val);
  18. }
  19. }
  20. int main() {
  21. struct TreeNode *t2 = malloc(sizeof(struct TreeNode));
  22. t2->val = 2;
  23. t2->left = NULL;
  24. t2->right = NULL;
  25. struct TreeNode *t3 = malloc(sizeof(struct TreeNode));
  26. t3->val = 3;
  27. t3->left = NULL;
  28. t3->right = NULL;
  29. struct TreeNode *t = malloc(sizeof(struct TreeNode));
  30. t->val = 1;
  31. t->left = t2;
  32. t->right = t3;
  33. printf("Expected: 1 (as root of tree)\n");
  34. printf("Got: %d\n", searchBST(t, 1)->val);
  35. return 0;
  36. }