/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public List<List<Integer>> findLeaves(TreeNode root) {
List<List<Integer>> rst = new ArrayList<>();
height(root, rst);
return rst;
}
private int height(TreeNode root, List<List<Integer>> rst) {
if (root == null) return -1;
int level = 1 + Math.max(height(root.left, rst), height(root.right, rst));
if (rst.size() < level + 1) {
rst.add(new ArrayList<>());
}
rst.get(level).add(root.val);
return level;
}
}