一个简单的递归求二叉树最小深度的代码
class Solution {
public:
int min(int x,int y){
return(x>y? y:x);
}
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
if(root->left==NULL&&root->right==NULL){
return 1;
}
else if(root->left==NULL){
return 1+minDepth(root->right);
}
else if(root->right==NULL){
return 1+minDepth(root->left);
}
else return min(1+minDepth(root->left),1+minDepth(root->right));
}
};