Skip to content

Commit

Permalink
Depth of binary tree
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 26, 2017
1 parent aab7f22 commit 1663c26
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 104_maximum_depth_of_binary_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test bst_depth.c
24 changes: 24 additions & 0 deletions 104_maximum_depth_of_binary_tree/bst_depth.c
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;
}
2 changes: 2 additions & 0 deletions 111_minimum_depth_of_binary_tree/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test bst_depth.c
32 changes: 32 additions & 0 deletions 111_minimum_depth_of_binary_tree/bst_depth.c
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;
}

0 comments on commit 1663c26

Please sign in to comment.