#include <frequently-used-code-snippets.h>
#define NOT_LEAF_NODE -1
#define MIN_TWO(a, b) (a < b ? a : b)
bool isLeafNode(TreeNode *node) {
return (node->left == NULL && node->right == NULL);
}
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) {
return 0;
}
if (isLeafNode(root)) {
return 1;
}
int leftMinDepth = NOT_LEAF_NODE;
if (root->left != NULL) {
leftMinDepth = minDepth(root->left);
}
int rightMinDepth = NOT_LEAF_NODE;
if (root->right != NULL) {
rightMinDepth = minDepth(root->right);
}
if (leftMinDepth != NOT_LEAF_NODE && rightMinDepth == NOT_LEAF_NODE) {
return leftMinDepth + 1;
}
if (leftMinDepth == NOT_LEAF_NODE && rightMinDepth != NOT_LEAF_NODE) {
return rightMinDepth + 1;
}
else {
return MIN_TWO(leftMinDepth, rightMinDepth) + 1;
}
}
};
int main() {
TreeNode A(5);
TreeNode B(4);
TreeNode C(8);
TreeNode D(11);
TreeNode E(13);
TreeNode F(4);
TreeNode G(7);
TreeNode H(2);
TreeNode I(1);
A.left = &B;
A.right = &C;
B.left = &D;
C.left = &E;
C.right = &F;
D.left = &G;
D.right = &H;
F.right = &I;
Solution sol;
cout << sol.minDepth(&A) << endl;
}