原题链接
class Solution { List<List<Integer>> result = new ArrayList<List<Integer>>(); List<List<Integer>> overturn = new ArrayList<List<Integer>>(); public List<List<Integer>> levelOrderBottom(TreeNode root) { checkfun(root); //返回结果 return overturn; } public void checkfun(TreeNode node){ if(node == null) return; Queue<TreeNode> que = new LinkedList<TreeNode>(); que.offer(node); while(!que.isEmpty()){ int len = que.size(); List<Integer> itemList = new ArrayList<>(); while(len > 0){ TreeNode tempNode = que.poll(); itemList.add(tempNode.val); if(tempNode.left != null) que.offer(tempNode.left); if(tempNode.right != null) que.offer(tempNode.right); len--; } result.add(itemList); } for(int i = result.size() - 1; i >= 0; i--){ overturn.add(result.get(i)); } } }