Input: [1,2,3,4,5]
1
/ \
2 3
/ \
4 5
Output: [[4,5,3],[2],[1]]
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
classSolution{
public List<List<Integer>>findLeaves(TreeNode node){
LinkedList<TreeNode> queue = new LinkedList<>();
queue.add(node);
Map<Integer,List<Integer>> map = new HashMap<>();
int h = -1;
while(!queue.isEmpty()){
h++;
System.out.println("h=="+h);
int size = queue.size();
List<Integer> l = new ArrayList<>();
for(int i=0;i<size;i++){
TreeNode tmpt = queue.poll();
l.add(tmpt.val);
System.out.println("val=="+tmpt.val);
if(tmpt.left != null){
queue.add(tmpt.left);
}
if(tmpt.right != null){
queue.add(tmpt.right);
}
}
map.put(h,l);
}
List<List<Integer>> result = new ArrayList<>();
for(Integer k:map.keySet()){
System.out.println("k=="+k);
List<Integer> l = map.get(k);
for(Integer i:l){
System.out.print(i);
}
System.out.println("");
result.put(k,l);
}
return result;
}