-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjava37.java
More file actions
66 lines (42 loc) · 1.29 KB
/
java37.java
File metadata and controls
66 lines (42 loc) · 1.29 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
/*✅ 1022. Sum of Root To Leaf Binary Numbers (LeetCode)
This problem gives a binary tree where each node contains either 0 or 1.
Every root-to-leaf path represents a binary number.
We need to return the sum of all those binary numbers in decimal form.
🔎 Key Idea
For every path:
Start from root.
At each node:
current = current * 2 + node.val
(This is equivalent to left shift in binary)
When you reach a leaf node, add the current value to the total sum.
🧠 Why This Works
If path is:
1 → 0 → 1
Binary calculation:
((1 * 2 + 0) * 2 + 1)
= (2 * 2 + 1)
= 5
Which is 101₂ = 5.
💻 Java Solution (DFS)*/
class Solution {
public int sumRootToLeaf(TreeNode root) {
return dfs(root, 0);
}
private int dfs(TreeNode node, int current) {
if (node == null) {
return 0;
}
// Form binary number
current = current * 2 + node.val;
// If leaf node, return value
if (node.left == null && node.right == null) {
return current;
}
// Recur for left and right
return dfs(node.left, current) + dfs(node.right, current);
}
}
/*⏱ Time Complexity
O(N) → Visit each node once.
📦 Space Complexity
O(H) → Recursive stack (H = height of tree)*/