LeetCode #111:Minimum Depth of Binary Tree
2018-03-01 11:21
316 查看
与求二叉树的最大深度类似,这道题是求二叉树的最小深度,深度定义为:从根节点到叶子节点的路径上的结点个数,当然,最小深度所在的路径即为从根节点到叶子结点所有路径中结点最少的路径,C++递归实现如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: int minDepth(TreeNode* root) { if(root == NULL) return 0; else if(root->left == NULL) return minDepth(root->right) + 1; else if(root->right == NULL) return minDepth(root->left) + 1; int minL = minDepth(root->left); int minR = minDepth(root->right); return minL < minR ? minL + 1 : minR + 1; } };
相关文章推荐
- [LeetCode]题解(python):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#104, 111]Maximum Depth of Binary Tree, Minimum Depth of Binary Tree
- LeetCode(111) Minimum Depth of Binary Tree解题报告
- leetcode111~Minimum Depth of Binary Tree
- leetcode_111_Minimum Depth of Binary Tree
- leetcode 111 Minimum Depth of Binary Tree
- [Leetcode 10] 111 Minimum Depth of Binary Tree
- leetcode 111 —— Minimum Depth of Binary Tree
- leetcode-111 Minimum Depth of Binary Tree
- LeetCode111 Minimum Depth of Binary Tree
- [leetcode 111] Minimum Depth of Binary Tree
- [leetcode-111]Minimum Depth of Binary Tree(C语言)
- <LeetCode OJ> (104 / 111) Maximum & Minimum Depth of Binary Tree
- leetcode111——Minimum Depth of Binary Tree
- [leetcode111].Minimum Depth of Binary Tree
- leetcode111---Minimum Depth of Binary Tree