This commit is contained in:
12
src/s0111_minimum_depth_of_binary_tree.cpp
Normal file
12
src/s0111_minimum_depth_of_binary_tree.cpp
Normal file
@@ -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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user