Minimum Depth of Binary Tree
void helper(TreeNode *root,unsigned int curDepth,unsigned int *minDepth) { if(curDepth>=*minDepth-1) return; curDepth++; if(!root->left&&!root->right) { *minDepth = curDepth; return; }else { if(root->left) helper(root->left,curDepth,minDepth); if(root->right) helper(root->right,curDepth,minDepth); } } int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return 0; unsigned int minDepth = (unsigned int)-1; helper(root,0,&minDepth); return minDepth; }
second_time:
int minDepth(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if(!root) return 0; if(!root->left&&!root->right) return 1; int ld = INT_MAX,rd = INT_MAX; if(root->left) ld = minDepth(root->left); if(root->right) rd = minDepth(root->right); return min(ld,rd)+1; }