This commit is contained in:
parent
393ddb83df
commit
38d44a6fa8
15
include/s0111_minimum_depth_of_binary_tree.hpp
Normal file
15
include/s0111_minimum_depth_of_binary_tree.hpp
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#ifndef S0111_MINIMUM_DEPTH_OF_BINARY_TREE_HPP
|
||||||
|
#define S0111_MINIMUM_DEPTH_OF_BINARY_TREE_HPP
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
|
#include "structures.hpp"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class S0111 {
|
||||||
|
public:
|
||||||
|
int minDepth(TreeNode* root);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user