瀏覽代碼

Latest problem done

master
Lachlan Jacob 5 年之前
父節點
當前提交
452714d230
共有 3 個檔案被更改,包括 67 行新增0 行删除
  1. 39
    0
      700/main.c
  2. 23
    0
      700/problem.txt
  3. 5
    0
      700/run.sh

+ 39
- 0
700/main.c 查看文件

@@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

struct TreeNode* searchBST(struct TreeNode*, int);
struct TreeNode* searchBST(struct TreeNode* root, int val){
if (root == NULL) {
return NULL;
} else if (root->val == val) {
return root;
} else if (root->val > val) {
return searchBST(root->left, val);
} else {
return searchBST(root->right, val);
}
}

int main() {
struct TreeNode *t2 = malloc(sizeof(struct TreeNode));
t2->val = 2;
t2->left = NULL;
t2->right = NULL;
struct TreeNode *t3 = malloc(sizeof(struct TreeNode));
t3->val = 3;
t3->left = NULL;
t3->right = NULL;
struct TreeNode *t = malloc(sizeof(struct TreeNode));
t->val = 1;
t->left = t2;
t->right = t3;
printf("Expected: 1 (as root of tree)\n");
printf("Got: %d\n", searchBST(t, 1)->val);
return 0;
}

+ 23
- 0
700/problem.txt 查看文件

@@ -0,0 +1,23 @@
Given the root node of a binary search tree (BST) and a value. You need to find the node in the BST that the node's value equals the given value. Return the subtree rooted with that node. If such node doesn't exist, you should return NULL.

For example,

Given the tree:
4
/ \
2 7
/ \
1 3

And the value to search: 2

You should return this subtree:

2
/ \
1 3

In the example above, if we want to search the value 5, since there is no node with value 5, we should return NULL.

Note that an empty tree is represented by NULL, therefore you would see the expected output (serialized tree format) as [], not null.


+ 5
- 0
700/run.sh 查看文件

@@ -0,0 +1,5 @@
#!/bin/bash

gcc -o main main.c
./main
rm main

Loading…
取消
儲存