二叉树最大深度和最小深度
int minDepth(TreeNode * root)
{
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
int left = minDepth(root->left) + 1;
int right = minDepth(root->right) + 1;
return Math.min(left,right);
}
int maxDepth(TreeNode *root)
{
if(root == NULL)
return 0;
if(root->left == NULL && root->right == NULL)
return 1;
int left = maxDepth(root->left) + 1;
int right = maxDepth(root->right) + 1;
return Math.max(left,right);
}
还没有评论,来说两句吧...