|
1 |
| -package com.fishercoder.solutions; |
2 |
| - |
3 |
| -import com.fishercoder.common.classes.TreeNode; |
4 |
| - |
5 |
| -import java.util.ArrayList; |
6 |
| -import java.util.Collections; |
7 |
| -import java.util.LinkedList; |
8 |
| -import java.util.List; |
9 |
| -import java.util.Queue; |
10 |
| - |
11 |
| -/**107. Binary Tree Level Order Traversal II |
12 |
| -
|
13 |
| -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). |
14 |
| -
|
15 |
| -For example: |
16 |
| -Given binary tree [3,9,20,null,null,15,7], |
17 |
| -
|
18 |
| - 3 |
19 |
| - / \ |
20 |
| - 9 20 |
21 |
| - / \ |
22 |
| - 15 7 |
23 |
| -
|
24 |
| -return its bottom-up level order traversal as: |
25 |
| -
|
26 |
| -[ |
27 |
| - [15,7], |
28 |
| - [9,20], |
29 |
| - [3] |
30 |
| -] |
31 |
| -*/ |
32 |
| - |
33 |
| -public class _107 { |
34 |
| - public static class Solution1 { |
35 |
| - public List<List<Integer>> levelOrder(TreeNode root) { |
36 |
| - List<List<Integer>> result = new ArrayList(); |
37 |
| - if (root == null) { |
38 |
| - return result; |
39 |
| - } |
40 |
| - |
41 |
| - Queue<TreeNode> q = new LinkedList(); |
42 |
| - q.offer(root); |
43 |
| - while (!q.isEmpty()) { |
44 |
| - List<Integer> thisLevel = new ArrayList<Integer>(); |
45 |
| - int qSize = q.size(); |
46 |
| - for (int i = 0; i < qSize; i++) { |
47 |
| - TreeNode curr = q.poll(); |
48 |
| - thisLevel.add(curr.val); |
49 |
| - if (curr.left != null) { |
50 |
| - q.offer(curr.left); |
51 |
| - } |
52 |
| - if (curr.right != null) { |
53 |
| - q.offer(curr.right); |
54 |
| - } |
55 |
| - } |
56 |
| - result.add(thisLevel); |
57 |
| - } |
58 |
| - Collections.reverse(result); |
59 |
| - return result; |
60 |
| - } |
61 |
| - } |
62 |
| -} |
| 1 | +package com.fishercoder.solutions; |
| 2 | + |
| 3 | +import com.fishercoder.common.classes.TreeNode; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.LinkedList; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Queue; |
| 10 | + |
| 11 | +/**107. Binary Tree Level Order Traversal II |
| 12 | +
|
| 13 | +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). |
| 14 | +
|
| 15 | +For example: |
| 16 | +Given binary tree [3,9,20,null,null,15,7], |
| 17 | +
|
| 18 | + 3 |
| 19 | + / \ |
| 20 | + 9 20 |
| 21 | + / \ |
| 22 | + 15 7 |
| 23 | +
|
| 24 | +return its bottom-up level order traversal as: |
| 25 | +
|
| 26 | +[ |
| 27 | + [15,7], |
| 28 | + [9,20], |
| 29 | + [3] |
| 30 | +] |
| 31 | +It can use 102's solution and do Collection.reverse; |
| 32 | +*/ |
| 33 | + |
| 34 | +public class _107 { |
| 35 | + public static class Solution1 { |
| 36 | + public List<List<Integer>> levelOrder(TreeNode root) { |
| 37 | + List<List<Integer>> result = new ArrayList(); |
| 38 | + if (root == null) { |
| 39 | + return result; |
| 40 | + } |
| 41 | + |
| 42 | + Queue<TreeNode> q = new LinkedList(); |
| 43 | + q.offer(root); |
| 44 | + while (!q.isEmpty()) { |
| 45 | + List<Integer> thisLevel = new ArrayList<Integer>(); |
| 46 | + int qSize = q.size(); |
| 47 | + for (int i = 0; i < qSize; i++) { |
| 48 | + TreeNode curr = q.poll(); |
| 49 | + thisLevel.add(curr.val); |
| 50 | + if (curr.left != null) { |
| 51 | + q.offer(curr.left); |
| 52 | + } |
| 53 | + if (curr.right != null) { |
| 54 | + q.offer(curr.right); |
| 55 | + } |
| 56 | + } |
| 57 | + result.add(thisLevel); |
| 58 | + } |
| 59 | + Collections.reverse(result); |
| 60 | + return result; |
| 61 | + } |
| 62 | + } |
| 63 | +} |
0 commit comments