【树】最小数目深度
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.
int run(TreeNode *root) {
if(root==nullptr) return 0;
if(root->left==nullptr&&root->right==nullptr)
return 1;
if(root->left==nullptr&&root->right!=nullptr)
return run(root->right)+1;
if(root->left!=nullptr&&root->right==nullptr)
return run(root->left)+1;
int l=1,r=1;
l+=run(root->left);
r+=run(root->right);
return l<r?l:r;
}
还没有评论,来说两句吧...