题目连接:Leetcode 111 Minimum Depth of Binary Tree
解题思路:递归,每次返回子树中层数低的一个加一。
/**
* 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;
if (root->left == NULL && root->right == NULL) return 1;
else if (root->left == NULL) return minDepth(root->right) + 1;
else if (root->right == NULL) return minDepth(root->left) + 1;
else return min(minDepth(root->left), minDepth(root->right)) + 1;
}
};
&spm=1001.2101.3001.5002&articleId=80740838&d=1&t=3&u=8a06580ec62c42e5bf5d66dba2c5a090)
1414

被折叠的 条评论
为什么被折叠?



