forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Leo Ma <[email protected]>
- Loading branch information
1 parent
aaa4cf0
commit d429234
Showing
4 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test valid_bst.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test recover_bst.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |