[LeetCode] Minimum Depth of Binary Tree - DFS
2013-01-24 03:37
609 查看
/** * 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; int left = minDepth(root->left); int right = minDepth(root->right); if (root->left && root->right) return min(left, right) + 1; else return left + right + 1; } };
Small Case: 8ms
Large Case: 92ms
Time: O(n)
Space: O(1)
DFS version.
相关文章推荐
- leetcode dfs Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree(DFS)
- LeetCode 111 Minimum Depth of Binary Tree (DFS)
- leetcode 111 Minimum Depth of Binary Tree(DFS)
- [leetcode][tree][dfs] Minimum Depth of Binary Tree
- LeetCode 111 Minimum Depth of Binary Tree(二叉树的最短深度)(BT、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(37)-Minimum Depth of Binary Tree
- LeetCode之Minimum Depth of Binary Tree
- [LeetCode]111 Minimum Depth of Binary Tree
- leetcode——111——Minimum Depth of Binary Tree
- 【Leetcode】【Easy】Minimum Depth of Binary Tree
- leetcode — minimum-depth-of-binary-tree
- LeetCode 41 Minimum Depth of Binary Tree
- [LeetCode] Minimum Depth of Binary Tree
- leetcode Minimum Depth of Binary Tree