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).
For example:
Given a binary tree [3,9,20,null,null,15,7],
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ class Solution { public List> levelOrderBottom(TreeNode root) { Queue nodes = new LinkedList(); List> res = new LinkedList(); if(root == null) return res; nodes.add(root); while(!nodes.isEmpty()){ List sub = new LinkedList(); int num = nodes.size(); for(int i=0;i
This siteOriginal articleAll follow "Attribution-NonCommercial-ShareAlike 4.0 License (CC BY-NC-SA 4.0)Please retain the following annotations when sharing or adapting:
Original author:Jake Tao,source:「107. Binary Tree Level Order Traversal II」