Skip to content

Commit

Permalink
Merge pull request youngyangyang04#107 from Joshua-Lu/patch-24
Browse files Browse the repository at this point in the history
更新 0112.路径总和 Java版本
  • Loading branch information
youngyangyang04 authored May 14, 2021
2 parents 2960297 + 5e40254 commit 4bf1ae7
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions problems/0112.路径总和.md
Original file line number Diff line number Diff line change
Expand Up @@ -305,25 +305,34 @@ public:


Java:
```java
```Java
class Solution {
private boolean flag = false;
public boolean hasPathSum(TreeNode root, int targetSum) {
has_2(root,targetSum);
return flag;
}

public void has_2 (TreeNode root, int count) {
if (root == null) return;
if (root.left == null && root.right == null && count == root.val) {
flag = true;
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.left != null) has_2(root.left,count - root.val);
if (root.right != null) has_2(root.right,count - root.val);
if (root.right != null) {
boolean right = hasPathSum(root.right, targetSum);
if (right) {// 已经找到
return true;
}
}
return false;
}
}
```

```

Python:

Expand Down

0 comments on commit 4bf1ae7

Please sign in to comment.