/**
* 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) return 0;
if (!root->left&&root->right) return minDepth(root->right) + 1;
if (root->left && !root->right) return minDepth(root->left) + 1;
return MIN(minDepth(root->left), minDepth(root->right)) + 1;
}
int MIN(int a, int b)
{
return a > b ? b : a;
}
};
111.二叉树的最小深度
猜你喜欢
转载自blog.csdn.net/weixin_43784305/article/details/103098915
今日推荐
周排行