leetcode 111 —— Minimum Depth of Binary Tree
来源:互联网 发布:阿里云学生认证被锁 编辑:程序博客网 时间:2024/05/01 20:14
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.
思路:BFS ,可以避免遍历整个数,当然最坏情况下是一样的
class Solution {public:int minDepth(TreeNode* root) {int level = 0;if (!root) return level;queue<TreeNode*> que;int cnt = 1; //当前层的节点数int nex = 0; //下一层的节点数que.push(root);while (true){level++;nex = 0;while (cnt--){ TreeNode* p = que.front();if (!p->left && !p->right) //叶子节点return level;if (p->left){ //不能用else if,又犯傻了que.push(p->left);nex++;}if (p->right){que.push(p->right);nex++;}que.pop();}cnt = nex;}}};
0 0
- leetcode 111 —— Minimum Depth of Binary Tree
- LeetCode(111)Minimum Depth of Binary Tree
- LeetCode: Minimum Depth of Binary Tree [111]
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- [LeetCode 111]Minimum Depth of Binary Tree
- leetCode #111 Minimum Depth of Binary Tree
- leetcode-111 Minimum Depth of Binary Tree
- leetcode 111 Minimum Depth of Binary Tree
- leetcode || 111、Minimum Depth of Binary Tree
- Leetcode[111]-Minimum Depth of Binary Tree
- leetcode[111]:Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- leetcode-111-Minimum Depth of Binary Tree
- Leetcode #111 Minimum Depth of Binary Tree
- leetcode 111: Minimum Depth of Binary Tree
- LeetCode 111: Minimum Depth of Binary Tree
- 利用双缓冲消除图片重画的闪烁现象
- leetCode #232 Implement Queue using Stacks
- Git Stash用法,方便临时处理bug时随时切换分支而不丢失修改
- construct express
- python生成随机用户名及密码方法
- leetcode 111 —— Minimum Depth of Binary Tree
- poj 2774
- Oracle EBS FSG报表迁移
- postgresql数据库psql控制台操作命令
- select()函数以及FD_ZERO、FD_SET、FD_CLR、FD_ISSET
- 数据库事务操作+数据库事务隔离级别
- 第6讲 Map,Tuple,Zip实战解析
- 一个自定义的典型的CRUD示例
- iOS 多线程