My solution in Java


  • 0
    T

    Welcome to discuss!^-^

    public int minDepth(TreeNode root) {
    		List<Integer> list = new ArrayList<Integer>();
    		if (root == null)
    			return 0;
    		minDepth(root, 1, list);
    		int min = list.get(0);
    		for (int i = 0; i < list.size(); i++) {
    			if (min > list.get(i))
    				min = list.get(i);
    		}
    		return min;
    	}
    
    	public void minDepth(TreeNode root, int depth, List<Integer> list) {
    		if (root == null) {
    			return;
    		}
    		if (isLeaf(root)) {
    			list.add(depth);
    		}
    		minDepth(root.left, depth + 1, list);
    		minDepth(root.right, depth + 1, list);
    	}
    
    	public boolean isLeaf(TreeNode root) {
    		if (root.left == null && root.right == null)
    			return true;
    		return false;
    	}

Log in to reply
 

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