leetcode dfs Minimum Depth of Binary Tree
2014-10-11 16:00
309 查看
Minimum Depth of Binary Tree
Total Accepted: 25609 TotalSubmissions: 86491My Submissions
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.
题意:求出二叉树的最小深度
思路:
minDepth(root) = 1 + min(minDepth(root->left), minDepth(root->right));
但如果 root -> left 或 root->right为空时,minDepth对他们的计算结果为返回 0 ,所以这两个要分别处理一下。
复杂度: 时间O(n) ,空间O(log n)
int minDepth(const TreeNode *root){
if(!root) return 0;
if(!root->left) return 1 + minDepth(root->right);
if(!root->right) return 1 + minDepth(root->left);
return 1 + min(minDepth(root->left), minDepth(root->right));
}
相关文章推荐
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- [leetcode][tree][dfs] Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree (DFS)
- [LeetCode] Minimum Depth of Binary Tree - DFS
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、DFS)(*)
- leetcode 111 Minimum Depth of Binary Tree(DFS)
- [面试真题] LeetCode:Minimum Depth of Binary Tree
- LeetCode:Minimum Depth of Binary Tree
- [LeetCode 111] - 二叉树的最小深度 (Minimum Depth of Binary Tree)
- LeetCode: Minimum Depth of Binary Tree
- leetcode 31: Minimum Depth of Binary Tree
- LeetCode题目笔记(一) -- Minimum Depth Of Binary Tree
- leetcode -- Minimum Depth of Binary Tree
- 【LeetCode with Python】 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