Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Leetcode 题解 - 树 437. Path Sum III Solution 需要更新 #1204

Open
ptrpengdev opened this issue Jul 26, 2022 · 8 comments
Open

Leetcode 题解 - 树 437. Path Sum III Solution 需要更新 #1204

ptrpengdev opened this issue Jul 26, 2022 · 8 comments

Comments

@ptrpengdev
Copy link

ptrpengdev commented Jul 26, 2022

437:新test case中 sum值会超过 Integer.MAX_VALUE 导致不过下面这个test case

Input:
[1000000000,1000000000,null,294967296,null,1000000000,null,1000000000,null,1000000000]
0
Output:
2
Expected:
0

新Solution: 可以把 pathSumStartWithRoot input 的int sum改成 long sum

public int pathSum(TreeNode root, int sum) {
    if (root == null) return 0;
    int ret = pathSumStartWithRoot(root, sum) + pathSum(root.left, sum) + pathSum(root.right, sum);
    return ret;
}
// sum type 从int改成long
private int pathSumStartWithRoot(TreeNode root, long sum) {
    if (root == null) return 0;
    int ret = 0;
    if (root.val == sum) ret++;
    ret += pathSumStartWithRoot(root.left, sum - root.val) + pathSumStartWithRoot(root.right, sum - root.val);
    return ret;
}
@yinyinfeiqiao
Copy link

yinyinfeiqiao commented Jul 26, 2022 via email

@HarryWord
Copy link

HarryWord commented Jul 26, 2022 via email

@ynuwm
Copy link

ynuwm commented Jul 26, 2022 via email

@tangdlit
Copy link

tangdlit commented Jul 26, 2022 via email

@IrenaChen33
Copy link

IrenaChen33 commented Jul 26, 2022 via email

@SmokingMouse
Copy link

SmokingMouse commented Jul 26, 2022 via email

@janko-zpeng
Copy link

janko-zpeng commented Jul 26, 2022 via email

@huyouen
Copy link

huyouen commented Jul 26, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants