|
3 | 3 | importcom.fishercoder.common.classes.TreeNode;
|
4 | 4 |
|
5 | 5 | /**
|
| 6 | + * 437. Path Sum III |
| 7 | + * |
6 | 8 | * You are given a binary tree in which each node contains an integer value.
|
7 | 9 |
|
8 | 10 | Find the number of paths that sum to a given value.
|
@@ -32,18 +34,20 @@ The path does not need to start or end at the root or a leaf, but it must go dow
|
32 | 34 |
|
33 | 35 | publicclass_437 {
|
34 | 36 |
|
35 |
| -publicintpathSum(TreeNoderoot,intsum) { |
36 |
| -if (root ==null) { |
37 |
| -return0; |
| 37 | +publicstaticclassSolution1 { |
| 38 | +publicintpathSum(TreeNoderoot,intsum) { |
| 39 | +if (root ==null) { |
| 40 | +return0; |
| 41 | + } |
| 42 | +returnpathSumFrom(root,sum) +pathSum(root.left,sum) +pathSum(root.right,sum); |
38 | 43 | }
|
39 |
| -returnpathSumFrom(root,sum) +pathSum(root.left,sum) +pathSum(root.right,sum); |
40 |
| - } |
41 | 44 |
|
42 |
| -privateintpathSumFrom(TreeNoderoot,intsum) { |
43 |
| -if (root ==null) { |
44 |
| -return0; |
| 45 | +privateintpathSumFrom(TreeNoderoot,intsum) { |
| 46 | +if (root ==null) { |
| 47 | +return0; |
| 48 | + } |
| 49 | +return (root.val ==sum ?1 :0) +pathSumFrom(root.left,sum -root.val) +pathSumFrom(root.right,sum -root.val); |
45 | 50 | }
|
46 |
| -return (root.val ==sum ?1 :0) +pathSumFrom(root.left,sum -root.val) +pathSumFrom(root.right,sum -root.val); |
47 | 51 | }
|
48 | 52 |
|
49 | 53 | }
|