题目描述
运行结果:
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;
if(root->left==NULL&&root->right==NULL) return 1;
if(root->left==NULL) return run(root->right)+1;
else if(root->right==NULL) return run(root->left)+1;
else
{
return 1+min(run(root->left),run(root->right));
}
}
};
运行结果: