Ejemplo n.º 1
0
 void findMinDepth(TreeNode *root, int depth, int &minDepth) {
 	
 	if (root->left == NULL && root->right == NULL) {
 		minDepth = depth < minDepth ? depth : minDepth;
 	} else {
 		if (root->left != NULL) {
 			findMinDepth(root->left, depth+1, minDepth);
 		}
 		if (root->right != NULL) {
 			findMinDepth(root->right, depth+1, minDepth);
 		}
 	}
 }
    void findMinDepth(TreeNode* root,int depth) {
        
        
        if (depth > min_level) {
            return;
        }
        if (root->left)
            findMinDepth(root->left, depth+1);
        if(root->right)
        findMinDepth(root->right, depth+1);
        if (!root->left && !root->right) {
            min_level =  min(min_level,depth); 
            return;

        }
    }
Ejemplo n.º 3
0
 int minDepth(TreeNode *root) {
     if (root == NULL)
     	return 0;
    	int minDepth = INT_MAX;
    	findMinDepth(root, 1, minDepth);
    	return minDepth;
 }
 int minDepth(TreeNode* root) {
     if(!root) {
         return 0;
     }
     
     findMinDepth(root,1);
     return min_level;
     
 }