GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — LeetCode/L112_Path_Sum.java at master · likevson/LeetCode

Expand file tree

/

L112_Path_Sum.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
53 lines (37 loc) · 898 Bytes
/

L112_Path_Sum.java

Copy path

File metadata and controls

53 lines (37 loc) · 898 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package LeetCode;
public class L112_Path_Sum {
boolean hasPath;
public boolean hasPathSum(TreeNode root, int sum) {
if (root == null) {
return false;
}
hasPath = false;
help(root, 0, sum);
return hasPath;
}
void help(TreeNode node, int cur, int sum) {
cur += node.val;
boolean isLeaf = (node.left == null) && (node.right == null);
if (cur == sum && isLeaf) {
hasPath = true;
}
if (node.left != null) {
help(node.left, cur, sum);
}
if (node.right != null) {
help(node.right, cur, sum);
}
cur -= node.val;
}
public boolean hasPathSum2(TreeNode root, int sum) {
if (root == null) {
return false;
}
if (root.left == null && root.right == null) {
return root.val == sum;
}
return (root.left != null && hasPathSum2(root.left, sum - root.val))
|| (root.right != null && hasPathSum2(root.right, sum
- root.val));
}
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.