Chinaunix首页 | 论坛 | 博客
  • 博客访问: 117337
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-24 16:22
文章存档

2014年(53)

我的朋友

分类: C/C++

2014-12-06 16:46:38

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 minDepth(TreeNode *root) {
  2.     if(root==NULL)
  3.         return 0;
  4.     int l=minDepth(root->left);
  5.     int r=minDepth(root->right);
  6.     if(l>0 && r>0)
  7.         return min(l,r)+1;
  8.     else
  9.         return l+r+1;
  10. }


阅读(860) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~