题目描述
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.
解题思路:
1,注意左右子树的递归
2,递归出口,left, right同时为空,分别为空
class Solution {
public:
int run(TreeNode *root) {
if(root == NULL)
return 0;
if(root->left == NULL)
return run(root->right) + 1;
if(root->right == NULL)
return run(root->left) + 1;
int lmin = run(root->left);
int rmin = run(root->right);
return min(lmin, rmin)+1;
}
};