Java 1ms solution beats 95%

  • 1
    	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>());
    		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.