From b83f378bee5f954c7d64fcac44fac03fb0387cf5 Mon Sep 17 00:00:00 2001 From: zhenzi Date: Sat, 15 May 2021 11:19:14 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=200112=E8=B7=AF=E5=BE=84?= =?UTF-8?q?=E5=92=8CJava=20=E7=AE=80=E6=B4=81=E8=A7=A3=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/0112.路径总和.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 1b75113e..ecbefd90 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -332,6 +332,19 @@ class Solution { } } +// LC112 简洁方法 +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { + + if (root == null) return false; // 为空退出 + + // 叶子节点判断是否符合 + if (root.left == null && root.right == null) return root.val == targetSum; + + // 求两侧分支的路径和 + return hasPathSum(root.left, targetSum - root.val) || hasPathSum(root.right, targetSum - root.val); + } +} ``` Python: