# July LeetCoding Challenge - 2

suntlee·2020년 7월 2일
0

목록 보기
2/9

# Day 2

## Binary Tree Level Order Traversal II

### 문제

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).

### 답(Java)

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode() {}
*     TreeNode(int val) { this.val = val; }
*     TreeNode(int val, TreeNode left, TreeNode right) {
*         this.val = val;
*         this.left = left;
*         this.right = right;
*     }
* }
*/
class Solution {
public List<List<Integer>> levelOrderBottom(TreeNode root) {
List<List<Integer>> list = new ArrayList<>();
if (root == null)
return list;
Queue<TreeNode> curLevel = new LinkedList<>();
while (!curLevel.isEmpty()) {
int size = curLevel.size();
List<Integer> valueList = new ArrayList<>();
for (int i = 0; i < size; ++i) {
TreeNode curNode = curLevel.poll();
if (curNode.left != null) {
curLevel.offer(curNode.left);
}
if (curNode.right != null) {
curLevel.offer(curNode.right);
}
}
}