Minimum Depth of Binary Tree
来源:互联网 发布:postman post传递json 编辑:程序博客网 时间:2024/06/03 20:10
class Solution {
public:
int minDepth(TreeNode* root) {
int res=0;
bool flag = false;
queue<TreeNode*> q;
TreeNode *t;
if (root == NULL)
return 0;
q.push(root);
q.push(NULL);
while (q.front() != NULL) {
res++;
while (q.front() != NULL) {
t = q.front();
q.pop();
if (t->left == NULL&&t->right == NULL) {
flag = true;
break;
}
if (t->left != NULL)
q.push(t->left);
if (t->right != NULL)
q.push(t->right);
}
if (flag)
break;
q.pop();
q.push(NULL);
}
return res;
}
};
public:
int minDepth(TreeNode* root) {
int res=0;
bool flag = false;
queue<TreeNode*> q;
TreeNode *t;
if (root == NULL)
return 0;
q.push(root);
q.push(NULL);
while (q.front() != NULL) {
res++;
while (q.front() != NULL) {
t = q.front();
q.pop();
if (t->left == NULL&&t->right == NULL) {
flag = true;
break;
}
if (t->left != NULL)
q.push(t->left);
if (t->right != NULL)
q.push(t->right);
}
if (flag)
break;
q.pop();
q.push(NULL);
}
return res;
}
};
阅读全文
0 0
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- Minimum Depth of Binary Tree
- 关于IntelliJ Idea搭建javaweb项目出现的Error filterStart错误解决
- 面试题之“度度熊”
- android调用dialog.hide()引起的输入事件派发错误问题追踪
- java所支持的国家和语音
- 继浸没液冷后,阿里再推国内首个锂电池服务器
- Minimum Depth of Binary Tree
- 【云星数据---Apache Flink实战系列(精品版)】:Apache Flink批处理API详解与编程实战010--DateSet实用API详解010
- 通过platform data 管理gpio资源--drv
- Ubuntu17.04+1080ti+cuda9+cudnn7+tensorflow1.4/1.3配置
- 【C#】类
- ADAS的公司分析对比--人工智能(辅助驾驶)
- Android自定义SeekBar,滑动时弹出气泡指示器显示进度
- Syntax error, parameterized types are only available if source level is 1.5
- Linux设备模型(3)_Uevent