-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPathSum112.java
165 lines (139 loc) · 4.07 KB
/
PathSum112.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* 112. Path Sum
Easy
Topics
Companies
Given the root of a binary tree and an integer targetSum, return true if the tree has a root-to-leaf path such that adding up all the values along the path equals targetSum.
A leaf is a node with no children.
Example 1:
Input: root = [5,4,8,11,null,13,4,7,2,null,null,null,1], targetSum = 22
Output: true
Explanation: The root-to-leaf path with the target sum is shown.
Example 2:
Input: root = [1,2,3], targetSum = 5
Output: false
Explanation: There two root-to-leaf paths in the tree:
(1 --> 2): The sum is 3.
(1 --> 3): The sum is 4.
There is no root-to-leaf path with sum = 5.
Example 3:
Input: root = [], targetSum = 0
Output: false
Explanation: Since the tree is empty, there are no root-to-leaf paths.
Constraints:
The number of nodes in the tree is in the range [0, 5000].
-1000 <= Node.val <= 1000
-1000 <= targetSum <= 1000
*/
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {}
TreeNode(int val) { this.val = val; }
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
public class PathSum112{
public static void main(String[] args) {
TreeNode node1 = new TreeNode(5);
TreeNode node2 = new TreeNode(4);
TreeNode node3 = new TreeNode(8);
TreeNode node4 = new TreeNode(11);
TreeNode node5 = new TreeNode(13);
TreeNode node6 = new TreeNode(4);
TreeNode node7 = new TreeNode(7);
TreeNode node8 = new TreeNode(2);
TreeNode node9 = new TreeNode(1);
// Link the nodes to form the tree structure
node1.left = node2;
node1.right = node3;
node2.left = node4;
node3.left = node5;
node3.right = node6;
node4.left = node7;
node4.right = node8;
node6.right = node9;
PathSum112 p = new PathSum112();
try {
// System.out.println(p.hasPathSum(node1, 22));
}
catch (Exception e) {
System.out.println(e);
}
}
}
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum){
MyStack stack = new MyStack();
if (root != null) {
stack.push(root);
}else{
return false;
}
return hasPathSum(targetSum, stack);
}
boolean hasPathSum(int targetSum, MyStack stack){
if (stack.isEmpty()) {
return false;
}
TreeNode currRoot = stack.pop();
if (currRoot.left == null && currRoot.right == null) {
if (currRoot.val == targetSum) {
return true;
}
}
if (currRoot.left != null) {
currRoot.left.val += currRoot.val;
stack.push(currRoot.left);
}
if (currRoot.right != null) {
currRoot.right.val += currRoot.val;
stack.push(currRoot.right);
}
return hasPathSum(targetSum, stack);
}
class MyStack{
Node topNode, nextNode;
class Node{
TreeNode element;
Node next;
}
boolean isEmpty(){
if (topNode == null) {
return true;
} else {
return false;
}
}
void push(TreeNode element){
if (topNode == null) {
topNode = new Node();
topNode.element = element;
}else{
nextNode = topNode;
topNode = new Node();
topNode.element = element;
topNode.next = nextNode;
}
}
TreeNode pop(){
Node head = topNode;
if (nextNode == null) {
topNode = null;
}
else if (nextNode.next == null) {
topNode = topNode.next;
nextNode = null;
}
else{
topNode = topNode.next;
nextNode = topNode.next;
}
return head.element;
}
}
}