My Recursive Solution


  • 1
    K
    public class Solution {
    
    int minimum = Integer.MAX_VALUE;
    
    public int minDepth(TreeNode root) {
       if(root == null) return 0; 
       minDepthHelper(root, 1);
       return minimum;
    }
    
    public void minDepthHelper(TreeNode node, int n)
    {
        if(node.left == null && node.right == null) 
        {
            if(n < minimum) minimum = n;
        }
        
        if(node.left != null) minDepthHelper(node.left, n+1);
        if(node.right != null) minDepthHelper(node.right, n+1);
    }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.