My java 1ms solution


  • 0
    T
    public class Solution {
    public int minDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        if((root.left==null)||(root.right==null)){
            return 1+max(minDepth(root.left),minDepth(root.right));
        }
        return 1+min(minDepth(root.left),minDepth(root.right));
    }
    
    private int min(int x,int y){
        return x>y?y:x;
    }
    
    private int max(int x,int y){
        return x<y?y:x;
    }
    

    }


  • 0
    S

    You can use Math.mix & Math.min


Log in to reply
 

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