-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSumRootToLeafNumbers.java
More file actions
334 lines (240 loc) · 9.4 KB
/
SumRootToLeafNumbers.java
File metadata and controls
334 lines (240 loc) · 9.4 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package Algorithms.BinaryTrees;
import DataStructures.BinaryTreeBasics;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 30 Nov 2025
* @link 129. Sum Root to Leaf Numbers <a href="https://leetcode.com/problems/sum-root-to-leaf-numbers/">LeetCode Link</a>
* @topics Tree, Binary Tree, DFS
* @companies Meta(23), Google(4), Amazon(2), Microsoft(9), Bloomberg(3)
*/
public class SumRootToLeafNumbers {
public static class TreeNode {int val; TreeNode left; TreeNode right; TreeNode(int val) {this.val = val;}}
public static void main(String[] args) {
TreeNode root = new TreeNode(4);
root.left = new TreeNode(9);
root.right = new TreeNode(0);
root.left.left = new TreeNode(5);
root.left.right = new TreeNode(1);
System.out.println("sumNumbers using Morris PreOrder Traversal " + sumNumbersUsingMorrisPreorderTraversal(root));
System.out.println("sumNumbers using Morris PreOrder DFS " + sumNumbersUsingPreOrderDfs(root));
System.out.println("sumNumbers using Morris Queue BFS " + sumNumbersUsingQueueBfs(root));
}
/**
* @see DataStructures.BinaryTreeBasics#morrisInOrderTraversal
* @TimeComplexity O(n)
* @SpaceComplexity O(1)
<pre>
4
/ \
9 0
/ \
5 1
Here, we have two pointers - 1.root/currRootNode & 2.predecessorNode
and we create link using ---> "predecessorNode.right = root"
1. if root.left != null then "predecessor = root.left" ---> i.e connect the current root node and predecessor vars
2. "predecessor.right = root" is the link
3. If we have no link, then go to the left sub-tree i.e root = root.left
4. If we have the link, break it and go to right sub-tree i.e root = root.right
--------- 1st iteration: ---------
root = 4
predecessor = 1 (curr root's left child's rightmost leaf node)
currSum = 0
totalSum = 9
(4r)<-----
/ \ |
9 0 |
/ \ |
5 (1p)-------
There is left child -->
1. Predecessor is one step left and then right till you can:
predecessor = 1
2. There is no link (predecessor.right != root)
--> set the link
--> go to the left subtree: root = root.left = 9 ---> new root
--------- 2nd iteration: ---------
root = 9
predecessor = 5 (curr root's left child's rightmost node)
currSum = 4
totalSum = 0
4 <--------
/ \ |
----> (9r) 0 |
| / \ |
--- (5p) 1 ---------
There is left child -->
1. Predecessor is one step left and then right till you can:
predecessor = 5
2. There is no link (predecessor.right != root)
--> set the link (5p.right = 9r)
--> go to the left subtree: root = root.left = 5 ---> new root
--------- 3rd iteration: ---------
root = 5
predecessor = null (as 5 node don't have any left child)
currSum = 49
totalSum = 0
4 <-------
/ \ |
----> 9 0 |
| / \ |
--- (5r) 1 ---------
There is no left child -->
1. go to the right side of curr root (it'll be 9 - prev root): root = root.right = 9 ---> new root
--------- 4th iteration: ---------
root = 9
predecessor = 5 (curr root's left child's rightmost node)
currSum = 495
totalSum = 495
4 <-------
/ \ |
----> (9r) 0 |
| / \ |
--- (5p) 1 ---------
4 <-------
/ \ |
(9r) 0 |
/ \ |
(5p) 1 ---------
There is left child -->
1. There is a link (predecessor.right == root)
--> break the link
--> now predecessor is leaf --> add 495 to totalSum
--> go to the right side of curr root --> root = root.right = 1 ---> new root
--------- 5th iteration: ---------
root = 1
predecessor = null (as 1 node don't have any left child)
currSum = 49
totalSum = 495
4 <-----
/ \ |
9 0 |
/ \ |
5 (1r)-------
There is no left child -->
1. go to the right side of curr root (it'll be 9 - prev root): root = root.right = 4 ---> new root
--------- 6th iteration: ---------
root = 4
predecessor = 1 (curr root's left child's rightmost node)
currSum = 491
totalSum = 986
4(r) <----
/ \ |
9 0 |
/ \ |
5 (1p)-------
4(r)
/ \
9 0
/ \
5 (1p)
There is left child -->
1. There is a link (predecessor.right == root)
--> break the link
--> now predecessor is leaf --> add 491 to totalSum
--> go to the right side of curr root --> root = root.right = 0 ---> new root
--------- 7th iteration: ---------
root = 0
predecessor = null (as 0 node don't have any left child)
currSum = 40
totalSum = 126
4
/ \
9 (0r)
/ \
5 1
There is no left child -->
1. Add 40 to totalSum as it's a leaf
2. Go to right side of curr root -> root = root.right = null ---> new root
--------- 8th iteration: ---------
As root = null, break the loop
</pre>
*/
public static int sumNumbersUsingMorrisPreorderTraversal(TreeNode root) {
int totalSum = 0, currSum = 0;
int steps;
TreeNode predecessor;
while (root != null) {
if (root.left == null) {
currSum = currSum * 10 + root.val;
if (root.right == null) totalSum += currSum; // root is leaf node -> ➕ totalSum
root = root.right;
} else {
predecessor = root.left;
steps = 1;
while (predecessor.right != null && predecessor.right != root) {
predecessor = predecessor.right;
steps++;
}
if (predecessor.right == null) { // --- NOT LINKED ---
currSum = currSum * 10 + root.val; // don't update totalSum yet -> as we do not know if the predecessor is leaf or not
predecessor.right = root;
root = root.left;
} else { // i.e., "predecessor.right == root" --- LINKED ---
if (predecessor.left == null) totalSum += currSum; // predecessor is leaf node -> ➕ totalSum
for (int i = 0; i < steps; ++i) currSum /= 10;
predecessor.right = null;
root = root.right;
}
}
}
return totalSum;
}
static int sum = 0;
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(h), where h is the height of the tree - for recursion stack
*/
public static int sumNumbersUsingPreOrderDfs(TreeNode root) {
sum = 0;
dfs(root, 0);
return sum;
}
private static void dfs(TreeNode node, int currSum) {
if (node == null) return;
currSum = currSum*10 + node.val;
if (node.left == null && node.right == null) {
sum += currSum;
return;
}
dfs(node.left, currSum);
dfs(node.right, currSum);
}
public static int sumNumbersUsingPreOrderDfs2(TreeNode root) {
return dfs2(root, 0);
}
private static int dfs2(TreeNode node, int currSum) {
if (node == null) return 0;
currSum = currSum*10 + node.val;
if (node.left == null && node.right == null) {
return currSum;
}
return dfs2(node.left, currSum) + dfs2(node.right, currSum);
}
/**
* @TimeComplexity O(n)
* @SpaceComplexity O(w), where w is the width of the tree - for queue
*/
public static int sumNumbersUsingQueueBfs(TreeNode root) {
if (root == null) return 0;
int totalSum = 0;
class Pair {final TreeNode node; final int currSum; Pair(TreeNode node, int currSum){this.node=node; this.currSum=currSum;}}
Queue<Pair> q = new LinkedList<>();
q.add(new Pair(root, 0));
while(!q.isEmpty()) {
int n = q.size();
while(n-- > 0) {
Pair pair = q.poll();
TreeNode node = pair.node;
int currSum = pair.currSum * 10 + node.val;
if (node.left == null && node.right == null) {
totalSum += currSum;
} else {
if (node.left != null) q.add(new Pair(node.left, currSum));
if (node.right != null) q.add(new Pair(node.right, currSum));
}
}
}
return totalSum;
}
}