Minimum Depth of Binary Tree

int minDepth(TreeNode* root) {

if(!root) return 0;

if(!root->left)//必须是leaf

return minDepth(root->right)+1;

if(!root->right)

return minDepth(root->left)+1;

return 1+min(minDepth(root->left),minDepth(root->right));

}

Last updated

Was this helpful?