【树】最小数目深度

缺乏、安全感 2022-07-16 23:38 75阅读 0赞

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.

  1. int run(TreeNode *root) {
  2. if(root==nullptr) return 0;
  3. if(root->left==nullptr&&root->right==nullptr)
  4. return 1;
  5. if(root->left==nullptr&&root->right!=nullptr)
  6. return run(root->right)+1;
  7. if(root->left!=nullptr&&root->right==nullptr)
  8. return run(root->left)+1;
  9. int l=1,r=1;
  10. l+=run(root->left);
  11. r+=run(root->right);
  12. return l<r?l:r;
  13. }

发表评论

表情:
评论列表 (有 0 条评论,75人围观)

还没有评论,来说两句吧...

相关阅读