From 38d44a6fa8f99627791353622e5e95f4e3d034d5 Mon Sep 17 00:00:00 2001 From: Sainnhe Park Date: Mon, 30 Jan 2023 21:00:44 +0800 Subject: [PATCH] s0111 --- include/s0111_minimum_depth_of_binary_tree.hpp | 15 +++++++++++++++ src/s0111_minimum_depth_of_binary_tree.cpp | 12 ++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 include/s0111_minimum_depth_of_binary_tree.hpp create mode 100644 src/s0111_minimum_depth_of_binary_tree.cpp 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; + } +}