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;
        
    }

  

posted @ 2013-05-29 16:37  summer_zhou  阅读(144)  评论(0编辑  收藏  举报