An straightforward way to get the answer


  • 1
    public class Solution {
        private int minDep = Integer.MAX_VALUE;
    	
    	public int minDepth(TreeNode root) {
    		if(root == null){
    			return 0; //no node
    		}
    		
    		helper(root, 0);
    		return minDep;
    		
    	}
    	
    	private void helper( TreeNode root, int depth ){
    		if(root == null)
    			return;
    		depth++;
    		//leaf node
    		if( root.left == null && root.right == null ){
    			if(depth < minDep){
    				minDep = depth;
    			}
    		}else {
    			helper(root.left, depth);
    			helper(root.right, depth);
    		}
    		
    	}
    }

Log in to reply
 

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