[LeetCode] 111. Minimum Depth of Binary Tree
2016-08-26 12:58
323 查看
思路:
这题很典型的BFS, 拿个队列记录这一层所有节点. 别用DFS, 虽然也能做对, 但对于这道题是很不好的解法.
这题很典型的BFS, 拿个队列记录这一层所有节点. 别用DFS, 虽然也能做对, 但对于这道题是很不好的解法.
int minDepth(TreeNode* root) { if (! root) return 0; queue<TreeNode*> q; q.push(root); int depth = 0; while (! q.empty()) { depty++; int size = q.size(); TreeNode* cur = q.front(); if (cur->left) q.push(cur->left); if (cur->right) q.push(cur->right); if (! cur->left && ! cur->right) return depth; q.pop(); } return depth; }
相关文章推荐
- [LeetCode] 111. Minimum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- Leetcode-111. Minimum Depth of Binary Tree
- leetcode 543. Diameter 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 104. Maximum Depth of Binary Tree
- LeetCode 111. Minimum Depth of Binary Tree
- [leetcode] 104. Maximum Depth of Binary Tree
- leetcode-111. Minimum Depth of Binary Tree
- LeetCode 543. Diameter of Binary Tree
- [LeetCode]--104. Maximum Depth of Binary Tree
- 【LeetCode】104. Maximum Depth of Binary Tree
- Leetcode 111. Minimum Depth of Binary Tree
- LeetCode——Binary Tree Inorder Traversal
- LeetCode 111. Minimum Depth of Binary Tree
- 【LeetCode】111.Minimum Depth of Binary Tree
- [LeetCode] 543. Diameter of Binary Tree