This is code to get all the root to leaf paths in a Binary Tree but it puts in all the paths concatenated into one path. What is going wrong with the recursive call?
private void rec(TreeNode root,List<Integer> l, List<List<Integer>> lists) {
if (root == null) return;
if (root.left == null && root.right == null ) {
l.add(root.val);
lists.add(l);
}
if (root.left != null) {
l.add(root.val);
rec(root.left,l,lists);
}
if (root.right != null) {
l.add(root.val);
rec(root.right,l,lists);
}
}