diff --git a/104_maximum_depth_of_binary_tree/Makefile b/104_maximum_depth_of_binary_tree/Makefile new file mode 100644 index 0000000..5a375a6 --- /dev/null +++ b/104_maximum_depth_of_binary_tree/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test bst_depth.c diff --git a/104_maximum_depth_of_binary_tree/bst_depth.c b/104_maximum_depth_of_binary_tree/bst_depth.c new file mode 100644 index 0000000..d3e7085 --- /dev/null +++ b/104_maximum_depth_of_binary_tree/bst_depth.c @@ -0,0 +1,24 @@ +#include +#include + +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; +} diff --git a/111_minimum_depth_of_binary_tree/Makefile b/111_minimum_depth_of_binary_tree/Makefile new file mode 100644 index 0000000..5a375a6 --- /dev/null +++ b/111_minimum_depth_of_binary_tree/Makefile @@ -0,0 +1,2 @@ +all: + gcc -O2 -o test bst_depth.c diff --git a/111_minimum_depth_of_binary_tree/bst_depth.c b/111_minimum_depth_of_binary_tree/bst_depth.c new file mode 100644 index 0000000..6a2626f --- /dev/null +++ b/111_minimum_depth_of_binary_tree/bst_depth.c @@ -0,0 +1,32 @@ +#include +#include + +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; +}