Skip to content

Commit aaa2023

Browse files
refactor 536
1 parent 2a5b4bd commit aaa2023

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

src/main/java/com/fishercoder/solutions/_536.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
public class _536 {
99

1010
public static class Solution1 {
11+
/**
12+
* recursive solution
13+
*/
1114
public TreeNode str2tree(String s) {
1215
if (s.equals("")) {
1316
return null;
@@ -38,6 +41,9 @@ public TreeNode str2tree(String s) {
3841
}
3942

4043
public static class Solution2 {
44+
/**
45+
* iterative solution
46+
*/
4147
public TreeNode str2tree(String s) {
4248
Deque<TreeNode> stack = new ArrayDeque<>();
4349
for (int i = 0, j = i; i < s.length(); i++, j = i) {

src/test/java/com/fishercoder/_536Test.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ public static void setup() {
2323
solution2 = new _536.Solution2();
2424
}
2525

26-
@Before
27-
public void setupForEachTest() {
28-
}
29-
3026
@Test
3127
public void test1() {
3228
s = "4(2(3)(1))(6(5))";
@@ -42,4 +38,20 @@ public void test2() {
4238
assertEquals(expected, solution1.str2tree(s));
4339
assertEquals(expected, solution2.str2tree(s));
4440
}
41+
42+
@Test
43+
public void test3() {
44+
s = "-4(2(3)(1))(6(5)(7))";
45+
expected = TreeUtils.constructBinaryTree(Arrays.asList(-4, 2, 6, 3, 1, 5, 7));
46+
assertEquals(expected, solution1.str2tree(s));
47+
assertEquals(expected, solution2.str2tree(s));
48+
}
49+
50+
@Test
51+
public void test4() {
52+
s = "4";
53+
expected = TreeUtils.constructBinaryTree(Arrays.asList(4));
54+
assertEquals(expected, solution1.str2tree(s));
55+
assertEquals(expected, solution2.str2tree(s));
56+
}
4557
}

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