classSolution { fun pathSum(root: TreeNode?, target: Int): List<List<Int>> { val ans= LinkedList<List<Int>>() val tmp=LinkedList<Int>() traceback(root,ans,tmp,target) return ans }
fun traceback(node:TreeNode?,ans:MutableList<List<Int>>,tmp:MutableList<Int>,target:Int){ if(node==null)return tmp.add(node.`val`) if(target-node.`val`==0&&node.left==null&&node.right==null){ ans.add(LinkedList(tmp)) }else{ traceback(node.left, ans, tmp, target-node.`val`) traceback(node.right, ans, tmp, target-node.`val`) } tmp.removeAt(tmp.size-1) } }