【发布时间】:2018-06-16 08:57:19
【问题描述】:
这是获取二叉树中所有根到叶路径的代码,但它将所有路径连接到一个路径中。递归调用出了什么问题?
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);
}
}
【问题讨论】:
标签: java algorithm arraylist binary-tree