Java recursive solution: 1ms


  • 0
    L
    public class Solution {
        public int minDepth(TreeNode root) {
    		if (root == null) {
    			return 0;
    		}
    		int minDepth = 0;
    		if (root.left == null || root.right == null) {
    			minDepth = Math.max(minDepth(root.left), minDepth(root.right));
    		} else {
    			minDepth = Math.min(minDepth(root.left), minDepth(root.right));
    		}
    		return 1 + minDepth;
    	}
    }

Log in to reply
 

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