LeetCode minDepth of 2 bin tree
2016-03-17 19:21
344 查看
111. Minimum Depth of Binary Tree
题目: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.
代码:
题目: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.
代码:
/** * 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) { // Start typing your C/C++ solution below // DO NOT write int main() function return minRec(root); } int minRec( TreeNode * root) { if(!root) return 0; if(root->left == NULL && root->right == NULL) return 1; int leftDepth = minRec(root->left); int rightDepth = minRec(root->right); if(root->left == NULL) return 1 + rightDepth; else if(root->right == NULL) return 1 + leftDepth; else return min(leftDepth,rightDepth) + 1; } };
相关文章推荐
- 第二周问题汇总
- mengento 数据库模型
- 404、500、502等HTTP状态码介绍
- android 内存处理工具
- C++ STL stack/queue
- java当中this应该怎么理解?
- 好文记载
- 消息系统Kafka介绍
- 100万并发连接服务器笔记之Erlang完成1M并发连接目标
- 微信用户昵称乱码解决
- 批量启动\停止ORACLE服务
- 100万并发连接服务器笔记之1M并发连接目标达成
- 堆的应用:解决海量数据,从复杂度优化
- 方法与数组
- 什么是备份策略?
- maven project
- Java中,jsp文件编译之后部署显示中文乱码
- 100万并发连接服务器笔记之测试端就绪
- 第3周项目4(3)年龄几何
- 【iOS】获取项目名和版本号