Java 1ms solution beats 95%


  • 1
    S
    	List<List<Integer>> list;
    	public List<List<Integer>> levelOrderBottom(TreeNode root) {
    		if (root == null) return null;
    		list = new ArrayList<List<Integer>>();
    		addToList(1, root);
    		return list;
    	}
    	
    	private void addToList(int level, TreeNode node) {
    		if (node == null) return;
    		if (list.size()<level) list.add(0, new ArrayList<Integer>());
    		list.get(list.size()-level).add(node.val);
    		addToList(level+1, node.left);
    		addToList(level+1, node.right);
    	}
    

Log in to reply
 

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