Skip to content

Commit

Permalink
Binary search tree
Browse files Browse the repository at this point in the history
Signed-off-by: Leo Ma <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 13, 2017
1 parent aaa4cf0 commit d429234
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 098_validate_binary_search_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test valid_bst.c
55 changes: 55 additions & 0 deletions 098_validate_binary_search_tree/valid_bst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

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

static int last_val = -1;

static bool traverse(struct TreeNode* node)
{
bool ret = true;

if (ret && node->left != NULL) {
ret = traverse(node->left);
}

if (last_val != -1 && node->val <= last_val) {
return false;
}
last_val = node->val;

if (ret && node->right != NULL) {
ret = traverse(node->right);
}
return ret;
}

static bool isValidBST(struct TreeNode* root)
{
if (root == NULL) return true;
last_val = -1;
return traverse(root);
}

int main(int argc, char **argv)
{
struct TreeNode root;
struct TreeNode left;
struct TreeNode right;
root.val = 2;
root.left = &left;
root.right = &right;
left.val = 1;
left.left = NULL;
left.right = NULL;
right.val = 3;
right.left = NULL;
right.right = NULL;
printf("%s\n", isValidBST(&root) ? "true" : "false");
return 0;
}
2 changes: 2 additions & 0 deletions 099_recover_binary_search_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test recover_bst.c
70 changes: 70 additions & 0 deletions 099_recover_binary_search_tree/recover_bst.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

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

static struct TreeNode *last = NULL;
static struct TreeNode *m1 = NULL;
static struct TreeNode *m2 = NULL;
static int wrong = 0;

static void traverse(struct TreeNode* node)
{
if (node->left != NULL) {
traverse(node->left);
}

if (last != NULL && node->val < last->val) {
if (++wrong == 2) {
int tmp = node->val;
node->val = m1->val;
m1->val = tmp;
}
m1 = last;
m2 = node;
}
last = node;

if (node->right != NULL) {
traverse(node->right);
}
}

static void recoverTree(struct TreeNode* root) {
if (root != NULL) {
last = NULL;
m1 = NULL;
m2 = NULL;
wrong = 0;
traverse(root);
if (wrong == 1) {
int tmp = m1->val;
m1->val = m2->val;
m2->val = tmp;
}
}
}

int main(int argc, char **argv)
{
struct TreeNode root;
struct TreeNode left;
struct TreeNode right;
root.val = 2;
root.left = &left;
root.right = &right;
left.val = 3;
left.left = NULL;
left.right = NULL;
right.val = 1;
right.left = NULL;
right.right = NULL;
recoverTree(&root);
printf("%d %d %d\n", root.val, left.val, right.val);
return 0;
}

0 comments on commit d429234

Please sign in to comment.