题目描述
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->right==NULL)//若右子树为空
return run(root->left)+1;
if(root->left==NULL)//若左子树为空
return run(root->right)+1;
else //左右子树都存在
return min( run(root->left)+1,run(root->right)+1);
}
};