From 3325d740d6ad1b7e58f3c7a4195340bfdd61ef16 Mon Sep 17 00:00:00 2001 From: Omooo <869759698@qq.com> Date: Wed, 8 Jul 2020 09:48:24 +0800 Subject: [PATCH] =?UTF-8?q?Update=20=E4=BA=8C=E5=8F=89=E6=A0=91=E7=9B=B8?= =?UTF-8?q?=E5=85=B3.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Algorithm/剑指 Offer/二叉树相关.md | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/blogs/Algorithm/剑指 Offer/二叉树相关.md b/blogs/Algorithm/剑指 Offer/二叉树相关.md index 6e5a0a3..c76b9a6 100644 --- a/blogs/Algorithm/剑指 Offer/二叉树相关.md +++ b/blogs/Algorithm/剑指 Offer/二叉树相关.md @@ -165,3 +165,32 @@ class Solution { } ``` +#### [ Offer 34. 二叉树中和为某一值的路径](https://leetcode-cn.com/problems/er-cha-shu-zhong-he-wei-mou-yi-zhi-de-lu-jing-lcof/) + +```java +class Solution { + + List> result = new ArrayList<>(); + List path = new ArrayList<>(); + + public List> pathSum(TreeNode root, int sum) { + path(root, sum); + return result; + } + + private void path(TreeNode root, int sum) { + if (root == null) { + return; + } + path.add(root.val); + int target = sum - root.val; + if (target == 0 && root.left == null && root.right == null) { + result.add(new ArrayList(path)); + } + path(root.left, target); + path(root.right, target); + path.remove(path.size() - 1); + } +} +``` +