[LeetCode] Minimum Depth of Binary Tree - BFS
来源:互联网 发布:网络三大奇书四大神书 编辑:程序博客网 时间:2024/05/01 12:50
/** * Definition for binary tree * 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 if (!root) return 0; queue<TreeNode *> q; q.push(root); q.push(NULL); int ret = 1; while (!q.empty()) { TreeNode *node = q.front(); q.pop(); if (!node) { ret++; q.push(NULL); } else { if (!node->left && !node->right) break; if (node->left) q.push(node->left); if (node->right) q.push(node->right); } } return ret; }};
Small Case: 8ms
Large Case: 92ms
Time: O(n)
Space: O(n)
BFS version.
- [LeetCode] Minimum Depth of Binary Tree - BFS
- LeetCode: Minimum Depth of Binary Tree
- [Leetcode] Minimum Depth of Binary Tree
- LeetCode : Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree - DFS
- 【leetcode】Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode]Minimum Depth of Binary Tree
- [Leetcode]Minimum Depth of Binary Tree
- [leetcode]Minimum Depth of Binary Tree
- Leetcode: Minimum Depth of Binary Tree
- LeetCode-Minimum Depth of Binary Tree
- [leetcode] Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 【Leetcode】Minimum Depth of Binary Tree
- LeetCode | Minimum Depth of Binary Tree
- LeetCode - Minimum Depth of Binary Tree
- 学习日记:如何写Makefile(二)——规则篇(中)
- Linux HugePages 配置 与 Oracle 性能关系说明
- [LeetCode] Flatten Binary Tree to Linked List
- [LeetCode] Path Sum
- [LeetCode] Path Sum II
- [LeetCode] Minimum Depth of Binary Tree - BFS
- [LeetCode] Minimum Depth of Binary Tree - DFS
- [LeetCode] Balanced Binary Tree
- 3DSMax常用快捷键
- [gpu pro]Fast, Stencil-Based Multiresolution Splatting for Indirect Illumination
- 一个非常好的学习SQL的网站 http://ezlearncomputer.com
- 一文讲清楚Windows Mobile和Wince(Windows Embedded CE)的字符集问题
- 43(65)---windows internal 6 mark divd alexa
- 对于解决一些VS 2010/2012 C++ 生成的exe在非编译环境电脑中缺失dll的方法