Skip to content

Commit fc3302f

Browse files
add 1022
1 parent ce1cfeb commit fc3302f

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Your ideas/fixes/algorithms are more than welcome!
2727

2828
| # | Title | Solutions | Time | Space | Video | Difficulty | Tag
2929
|-----|----------------|---------------|---------------|---------------|--------|-------------|-------------
30+
|1022|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1022.java) | O(n) | O(n) | |Easy|
3031
|1021|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1021.java) | O(n) | O(n) | |Easy|
3132
|1018|[Binary Prefix Divisible By 5](https://leetcode.com/problems/binary-prefix-divisible-by-5/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1018.java) | O(n) | O(1) | |Easy|
3233
|1013|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_1013.java) | O(n) | O(1) | |Easy|
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* Source: https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/
10+
*
11+
* 1022. Sum of Root To Leaf Binary Numbers
12+
*
13+
* Given a binary tree, each node has value 0 or 1. Each root-to-leaf path represents a binary number starting with the most significant bit. For example, if the path is 0 -> 1 -> 1 -> 0 -> 1, then this could represent 01101 in binary, which is 13.
14+
*
15+
* For all leaves in the tree, consider the numbers represented by the path from the root to that leaf.
16+
*
17+
* Return the sum of these numbers.
18+
*
19+
* Example 1:
20+
*
21+
* 1
22+
* / \
23+
* 0 1
24+
* / \ / \
25+
* 0 1 0 1
26+
*
27+
* Input: [1,0,1,0,1,0,1]
28+
* Output: 22
29+
* Explanation: (100) + (101) + (110) + (111) = 4 + 5 + 6 + 7 = 22
30+
*
31+
* Note:
32+
*
33+
* The number of nodes in the tree is between 1 and 1000.
34+
* node.val is 0 or 1.
35+
* The answer will not exceed 2^31 - 1.
36+
* */
37+
public class _1022 {
38+
public static class Solution1 {
39+
public int sumRootToLeaf(TreeNode root) {
40+
List<List<Integer>> paths = new ArrayList<>();
41+
dfs(root, paths, new ArrayList<>());
42+
int sum = 0;
43+
for (List<Integer> list : paths) {
44+
int num = 0;
45+
for (int i : list) {
46+
num = (num << 1) + i;
47+
}
48+
sum += num;
49+
}
50+
return sum;
51+
}
52+
53+
private void dfs(TreeNode root, List<List<Integer>> paths, List<Integer> path) {
54+
path.add(root.val);
55+
if (root.left != null) {
56+
dfs(root.left, paths, path);
57+
path.remove(path.size() - 1);
58+
}
59+
if (root.right != null) {
60+
dfs(root.right, paths, path);
61+
path.remove(path.size() - 1);
62+
}
63+
if (root.left == null && root.right == null) {
64+
paths.add(new ArrayList<>(path));
65+
}
66+
}
67+
}
68+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.common.utils.TreeUtils;
5+
import com.fishercoder.solutions._1022;
6+
import org.junit.BeforeClass;
7+
import org.junit.Test;
8+
9+
import java.util.Arrays;
10+
11+
import static junit.framework.Assert.assertEquals;
12+
13+
public class _1022Test {
14+
private static _1022.Solution1 solution1;
15+
private static TreeNode root;
16+
17+
@BeforeClass
18+
public static void setup() {
19+
solution1 = new _1022.Solution1();
20+
}
21+
22+
@Test
23+
public void test1() {
24+
root = TreeUtils.constructBinaryTree(Arrays.asList(1, 0, 1, 0, 1, 0, 1));
25+
TreeUtils.printBinaryTree(root);
26+
assertEquals(22, solution1.sumRootToLeaf(root));
27+
}
28+
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy