From a297f2eab6826ad2dd81feacaa4bb39234fd0684 Mon Sep 17 00:00:00 2001 From: Joshua <47053655+Joshua-Lu@users.noreply.github.com> Date: Fri, 14 May 2021 01:25:56 +0800 Subject: [PATCH] =?UTF-8?q?Update=200112.=E8=B7=AF=E5=BE=84=E6=80=BB?= =?UTF-8?q?=E5=92=8C.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加 0112.路径总和 Java版本 --- problems/0112.路径总和.md | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/problems/0112.路径总和.md b/problems/0112.路径总和.md index 718a2f5b..41d8b802 100644 --- a/problems/0112.路径总和.md +++ b/problems/0112.路径总和.md @@ -305,7 +305,34 @@ public: Java: +```Java +class Solution { + public boolean hasPathSum(TreeNode root, int targetSum) { + if (root == null) { + return false; + } + targetSum -= root.val; + // 叶子结点 + if (root.left == null && root.right == null) { + return targetSum == 0; + } + if (root.left != null) { + boolean left = hasPathSum(root.left, targetSum); + if (left) {// 已经找到 + return true; + } + } + if (root.right != null) { + boolean right = hasPathSum(root.right, targetSum); + if (right) {// 已经找到 + return true; + } + } + return false; + } +} +``` Python: