diff --git a/include/s0111_minimum_depth_of_binary_tree.hpp b/include/s0111_minimum_depth_of_binary_tree.hpp new file mode 100644 index 0000000..1e8e54c --- /dev/null +++ b/include/s0111_minimum_depth_of_binary_tree.hpp @@ -0,0 +1,15 @@ +#ifndef S0111_MINIMUM_DEPTH_OF_BINARY_TREE_HPP +#define S0111_MINIMUM_DEPTH_OF_BINARY_TREE_HPP + +#include + +#include "structures.hpp" + +using namespace std; + +class S0111 { + public: + int minDepth(TreeNode* root); +}; + +#endif diff --git a/src/s0111_minimum_depth_of_binary_tree.cpp b/src/s0111_minimum_depth_of_binary_tree.cpp new file mode 100644 index 0000000..8a548d4 --- /dev/null +++ b/src/s0111_minimum_depth_of_binary_tree.cpp @@ -0,0 +1,12 @@ +#include "s0111_minimum_depth_of_binary_tree.hpp" + +int S0111::minDepth(TreeNode* root) { + if (root == nullptr) return 0; + if (root->left == nullptr && root->right != nullptr) { + return minDepth(root->right) + 1; + } else if (root->left != nullptr && root->right == nullptr) { + return minDepth(root->left) + 1; + } else { + return min(minDepth(root->left), minDepth(root->right)) + 1; + } +}