Minimum Depth of Binary Tree
2016-05-03 09:44
274 查看
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.
The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { if(!root){ return 0; } if(!root->left && !root->right){ return 1; } if(!root->left){ return minDepth(root->right)+1; } if(!root->right){ return minDepth(root->left)+1; } int leftDepth = minDepth(root->left); int rightDepth = minDepth(root->right); return (leftDepth < rightDepth)?(leftDepth+1):(rightDepth+1); } };
相关文章推荐
- Linux 多线程文件复制
- MySQL内存分配
- Seconds_Behind_Master的计算
- innnodb 线程在做什么?
- Mysql Join_buffer_size的使用原理
- C 内存池的实现
- C实现队列
- mysqld执行的函数栈
- MySQL高性能以及高安全测试
- C 实现简单的栈
- Java web 之mysql连接
- iOS_导航栏的navigationBar.hidden与navigationBarHidden的区别
- dbms_scheduler介绍
- 在PHP中调用接口
- 数塔问题
- perl 操作mysql 数据库
- Drupal的主题----第一章:Drupal的介绍
- vs 2013 密钥
- 浏览器对网址中的&与&解释时间各不同
- Git命令大全