原题
寻找二叉树最短深度
思路:
这里用了dfs,beat 100%,4ms
class Solution
{
public:
int minDepth(TreeNode *root, int minNum = 0)
{
if (root == NULL)
{
return minNum == 0 ? minNum : 999999;
}
if (root->left == NULL && root->right == NULL)
return minNum + 1;
return min(minDepth(root->left, minNum + 1),
minDepth(root->right, minNum + 1));
}
};
网友评论