题目描述:
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 minDepth(TreeNode* root) {
if(root==NULL) return 0;
else if(root->left==NULL&&root->right==NULL)
return 1;
else if(root->left!=NULL&&root->right==NULL)
return 1+minDepth(root->left);
else if(root->left==NULL&&root->right!=NULL)
return 1+minDepth(root->right);
else if(root->left!=NULL&&root->right!=NULL)
return 1+min(minDepth(root->left),minDepth(root->right));
}
};