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: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
aab7f22
commit 1663c26
Showing
4 changed files
with
60 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 bst_depth.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,24 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct TreeNode { | ||
int val; | ||
struct TreeNode *left; | ||
struct TreeNode *right; | ||
}; | ||
|
||
static int maxDepth(struct TreeNode* root) | ||
{ | ||
if (root == NULL) { | ||
return 0; | ||
} | ||
int l = maxDepth(root->left) + 1; | ||
int r = maxDepth(root->right) + 1; | ||
return l > r ? l : r; | ||
} | ||
|
||
int main(void) | ||
{ | ||
printf("%d\n", maxDepth(NULL)); | ||
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 bst_depth.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,32 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct TreeNode { | ||
int val; | ||
struct TreeNode *left; | ||
struct TreeNode *right; | ||
}; | ||
|
||
static int minDepth(struct TreeNode* root) | ||
{ | ||
if (root == NULL) { | ||
return 0; | ||
} | ||
|
||
int l = minDepth(root->left) + 1; | ||
int r = minDepth(root->right) + 1; | ||
return l < r ? (l > 1 ? l : r) : (r > 1 ? r : l); | ||
} | ||
|
||
int main(void) | ||
{ | ||
struct TreeNode root, n1; | ||
root.val = 1; | ||
n1.val = 2; | ||
root.left = &n1; | ||
root.right = NULL; | ||
n1.left = NULL; | ||
n1.right = NULL; | ||
printf("%d\n", minDepth(&root)); | ||
return 0; | ||
} |