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==nullptr){
return 0;
}
int lc=run(root->left);
int rc=run(root->right);
if(lc==0){
return rc+1;
}
else if(rc==0){
return lc+1;
}
else
return min(lc,rc)+1;
}
};