leetcode 每日一题 104. Maximum Depth of Binary Tree
2016-03-03 16:24
357 查看
求二叉树的深度的题,可以用递归的DFS做,试了几次发现ac了····
后来发现还可以用BFS的队列做~ 代码使用的 http://blog.csdn.net/doc_sgl/article/details/11853959 有空要自己再敲一下
class Solution { public: int count=0; int maxDepth(TreeNode* root) { if(root==NULL) return 0; count=max(maxDepth(root->left),maxDepth(root->right)); return count+1; } };
后来发现还可以用BFS的队列做~ 代码使用的 http://blog.csdn.net/doc_sgl/article/details/11853959 有空要自己再敲一下
int maxDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(root == NULL) return 0; queue<TreeNode*> que; que.push(root); int count = 1; int depth = 0; while(!que.empty()) { TreeNode* tmp = que.front(); que.pop(); count--; if(tmp->left) que.push(tmp->left); if(tmp->right) que.push(tmp->right); if(count == 0) { depth++; count=que.size(); } } return depth; }
相关文章推荐
- UIImageView自适应图片大小
- Redis的Java客户端Jedis的八种调用方式(事务、管道、分布式…)介绍
- leetcode:Merge Two Sorted Lists 【Java】
- cocos3.9 【 Sublime Text 2 配置 】
- python while循环控制流语句结构与用法
- zabbix安装,关闭SELinux
- 日期控件如何显示出时分秒
- ubuntu操作技巧——如何下载安装搜狗输入法
- # _http_outgoing JS的错误: The header content contains invalid characters
- c/c++静态区 常量区 栈 堆
- GUISkin 自定义皮肤样式
- Chart.js
- [Unity热更新]更新lua脚本 (一)
- 第一篇博客-忽悠妹子的
- CORS其实很简单:NGINX 一招搞定针对特定域名和端口跨域,动态决定跨域到哪里
- Rx(函数响应式编程) Java,Rx Android,EventBus
- struts2日期转换在Win10上失败问题的分析和解决
- js工具---metadata.js
- MySQL在右表数据不唯一的情况下使用left join的方法
- 自己实现AtomicFloat的示例代码