-
Notifications
You must be signed in to change notification settings - Fork 2
/
PathSum1.java
57 lines (44 loc) · 882 Bytes
/
PathSum1.java
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
54
55
56
57
package com.tree2;
public class PathSum1 {
public static class TreeNode {
int value;
TreeNode left;
TreeNode right;
}
public static boolean pathSum(TreeNode p,int target){
if (p == null)
return false;
if (p.left != null && p.right != null){
if (p.value >= target)
return false;
else
{
return pathSum(p.left,target - p.value) || pathSum(p.right,target -p.value);
}
}else{
if (p.left != null){
if (p.value >= target)
return false;
else
{
return pathSum(p.left,target - p.value) ;
}
}
if (p.right != null){
if (p.value >= target)
return false;
else
{
return pathSum(p.right,target -p.value);
}
}
if (p.value == target)
return true;
else
return false;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}