MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0107/Solution.java:17–40  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/09 desc :

Source from the content-addressed store, hash-verified

15 * </pre>
16 */
17public class Solution {
18 public List<List<Integer>> levelOrderBottom(TreeNode root) {
19 List<List<Integer>> list = new LinkedList<>();
20 helper(list, root, 0);
21 return list;
22 }
23
24 private void helper(List<List<Integer>> list, TreeNode root, int level) {
25 if (root == null) return;
26 if (level >= list.size()) {
27 list.add(0, new LinkedList<>());
28 }
29 helper(list, root.left, level + 1);
30 helper(list, root.right, level + 1);
31 list.get(list.size() - level - 1).add(root.val);
32 }
33
34 public static void main(String[] args) {
35 Solution solution = new Solution();
36 System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[]")));
37 System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[1,2,2,3,4,4,3]")));
38 System.out.println(solution.levelOrderBottom(TreeNode.createTestData("[9,-42,-42,null,76,76,null,null,13,null,13]")));
39 }
40}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected