Leetcode 111 Solution
This article provides solution to leetcode question 111 (minimum-depth-of-binary-tree)
Access this page by simply typing in "lcs 111" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/minimum-depth-of-binary-tree
Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL)
return 0;
int left_depth = minDepth(root->left);
int right_depth = minDepth(root->right);
if (left_depth == 0 && right_depth == 0)
return 1;
else if (left_depth == 0)
return right_depth + 1;
else if (right_depth == 0)
return left_depth + 1;
else
return min(left_depth, right_depth) + 1;
}
};