leetcode/src/s0111_minimum_depth_of_bina...

13 lines
400 B
C++

#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;
}
}