Minimum Depth of Binary Tree
2015-07-09 11:09
274 查看
https://leetcode.com/problems/minimum-depth-of-binary-tree/
/** * 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) { queue<TreeNode *> q1; if(root==NULL) return 0; q1.push(root); int depth=0; bool flag=0; while(!q1.empty()) { depth++; queue<TreeNode *> q2; while(!q1.empty()) { TreeNode * temp=q1.front(); q1.pop(); if(temp->left==NULL&&temp->right==NULL) { flag=1; break; } if(temp->left!=NULL) q2.push(temp->left); if(temp->right!=NULL) q2.push(temp->right); } q1=q2; if(flag==1) break; } return depth; } };
相关文章推荐
- 再谈mobile web retina 下 1px 边框解决方案
- ASP.NET - 对URL传递的值进行编码Server.UrlEncode()
- 如何在Vmware Horizon View 6.1中支持Windows XP
- JSTL开发实例详解
- GRE写作必备句型
- java web开发一个帐号同一时间只能一个人登录的功能
- Source Insight,修改字体
- 使用工具(JD-GUI和APKIDE)反编译JAR的方法
- Qt 创建一个线程执行某个函数
- 数据库定期备份shell
- PHP生成word的三种方式
- 项目总结
- 基于bootstarp的Dailog
- 如何去设计一个自适应的网页设计或HTMl5
- map的erase()释放内存
- SAPUI5-Button
- CSRF(跨站请求伪造)详细说明
- linux上SVN解决冲突的办法
- HTML 5 拖放
- table checked row