題目描述
Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
題意是尋找二叉樹的最小高度,遞歸解決思路如下。
class Solution {
public:
int run(TreeNode *root) {
if(root == NULL)
return 0;
int lmin = run(root->left);
int rmin = run(root->right);
if(lmin==0 && rmin == 0)
return 1;
if(lmin == 0)
lmin = INT_MAX;
if(rmin == 0)
rmin = INT_MAX;
return min(lmin, rmin)+1;
}
};