diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 283a9913..da62452c 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -411,6 +411,31 @@ class solution { } ``` +```java +// 解法2 +class Solution { + List> result; + LinkedList path; + public List> pathSum (TreeNode root,int targetSum) { + result = new LinkedList<>(); + path = new LinkedList<>(); + travesal(root, targetSum); + return result; + } + private void travesal(TreeNode root, int count) { + if (root == null) return; + path.offer(root.val); + count -= root.val; + if (root.left == null && root.right == null && count == 0) { + result.add(new LinkedList<>(path)); + } + travesal(root.left, count); + travesal(root.right, count); + path.removeLast(); // 回溯 + } +} +``` + ## python 0112.路径总和