// 记录结果 LinkedList<List<Integer>> res = new LinkedList<>(); // 记录路径 LinkedList<Integer> path = new LinkedList<>(); public List<List<Integer>> pathSum(TreeNode root, int sum) { recur(root, sum); return res; } public void recur(TreeNode root, int tar) { // 终止条件 if(root == null) { return; } path.add(root.val); tar -= root.val; // 满足到达叶子节点且路径和为sum if(tar == 0 && root.left == null && root.right == null) { // 结果集加入该路径 res.add(new LinkedList<>(path)); } // 前序遍历 recur(root.left, tar); recur(root.right, tar); // 向上回溯 path.removeLast(); }
剑指Offer-34二叉树中和为某一值的路径
猜你喜欢
转载自blog.csdn.net/a792396951/article/details/113858766
今日推荐
周排行