/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */classSolution{publicList<List<Integer>>levelOrderBottom(TreeNoderoot){LinkedList<List<Integer>>levelOrder=newLinkedList<>();if(root==null){returnlevelOrder;}//创建队列Queue<TreeNode>queue=newLinkedList<TreeNode>();//将根节点加入到队列中queue.offer(root);while(!queue.isEmpty()){List<Integer>level=newArrayList<>();//获取当前层的TreeNode的个数intsize=queue.size();//遍历取出当前层的所有TreeNode,并将下一层的加入for(inti=0;i<size;i++){TreeNodenode=queue.poll();level.add(node.val);if(node.left!=null){queue.offer(node.left);}if(node.right!=null){queue.offer(node.right);}}levelOrder.addFirst(level);}returnlevelOrder;}}