Skip to content

Commit d1fab0e

Browse files
committedAug 16, 2017
refactor 536
1 parent e6f7cc9 commit d1fab0e

File tree

3 files changed

+63
-21
lines changed

3 files changed

+63
-21
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Your ideas/fixes/algorithms are more than welcome!
108108
|539|[Minimum Time Difference](https://leetcode.com/problems/minimum-time-difference/)|[Solution](../master/src/main/java/com/fishercoder/solutions/MinimumTimeDifference.java) | O(n) |O(1) | Medium | String
109109
|538|[Convert BST to Greater Tree](https://leetcode.com/problems/convert-bst-to-greater-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_538.java) | O(n) |O(h) | Medium | Tree
110110
|537|[Complex Number Multiplication](https://leetcode.com/problems/complex-number-multiplication/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_537.java) | O(1) |O(1) | Medium | Math, String
111-
|536|[Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_536.java) | O(n) |O(h) | Medium | Recursion
111+
|536|[Construct Binary Tree from String](https://leetcode.com/problems/construct-binary-tree-from-string/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_536.java) | O(n) |O(h) | Medium | Recursion, Stack
112112
|535|[Encode and Decode TinyURL](https://leetcode.com/problems/encode-and-decode-tinyurl/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_535.java) | O(1) |O(n) | Medium | Design
113113
|533|[Lonely Pixel II](https://leetcode.com/problems/lonely-pixel-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_533.java) | O(m*n) |O(m) (m is number of rows) | Medium | HashMap
114114
|532|[K-diff Pairs in an Array](https://leetcode.com/problems/k-diff-pairs-in-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_532.java) | O(n) |O(n) | Easy | HashMap

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

+55-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,12 @@
22

33
import com.fishercoder.common.classes.TreeNode;
44

5+
import java.util.ArrayDeque;
6+
import java.util.Deque;
7+
58
/**
9+
* 536. Construct Binary Tree from String
10+
*
611
* You need to construct a binary tree from a string consisting of parenthesis and integers.
712
The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis.
813
The integer represents the root's value and a pair of parenthesis contains a child binary tree with the same structure.
@@ -22,27 +27,60 @@
2227
There will only be '(', ')', '-' and '0' ~ '9' in the input string.
2328
An empty tree is represented by "" instead of "()".
2429
*/
30+
2531
public class _536 {
2632

27-
public TreeNode str2tree(String s) {
28-
if (s.equals("")) return null;
29-
int firstParen = s.indexOf("(");
30-
int val = firstParen == -1 ? Integer.parseInt(s) : Integer.parseInt(s.substring(0, firstParen));
31-
TreeNode cur = new TreeNode(val);
32-
if (firstParen == -1) return cur;
33-
int start = firstParen;
34-
int leftParenCount = 0;
35-
for (int i = start; i < s.length(); i++) {
36-
if (s.charAt(i) == '(') leftParenCount++;
37-
else if (s.charAt(i) == ')') leftParenCount--;
38-
if (leftParenCount == 0 && start == firstParen) {
39-
cur.left = str2tree(s.substring(start + 1, i));
40-
start = i + 1;
41-
} else if (leftParenCount == 0) {
42-
cur.right = str2tree(s.substring(start + 1, i));
33+
public static class Solution1 {
34+
public TreeNode str2tree(String s) {
35+
if (s.equals("")) return null;
36+
int firstParen = s.indexOf("(");
37+
int val = firstParen == -1 ? Integer.parseInt(s) : Integer.parseInt(s.substring(0, firstParen));
38+
TreeNode cur = new TreeNode(val);
39+
if (firstParen == -1) return cur;
40+
int start = firstParen;
41+
int leftParenCount = 0;
42+
for (int i = start; i < s.length(); i++) {
43+
if (s.charAt(i) == '(') {
44+
leftParenCount++;
45+
} else if (s.charAt(i) == ')') {
46+
leftParenCount--;
47+
}
48+
if (leftParenCount == 0 && start == firstParen) {
49+
cur.left = str2tree(s.substring(start + 1, i));
50+
start = i + 1;
51+
} else if (leftParenCount == 0) {
52+
cur.right = str2tree(s.substring(start + 1, i));
53+
}
54+
}
55+
return cur;
56+
}
57+
}
58+
59+
public static class Solution2 {
60+
public TreeNode str2tree(String s) {
61+
Deque<TreeNode> stack = new ArrayDeque<>();
62+
for (int i = 0, j = i; i < s.length(); i++, j = i) {
63+
char c = s.charAt(i);
64+
if (c == ')') {
65+
stack.pop();
66+
} else if (c >= '0' && c <= '9' || c == '-') {
67+
while (i + 1 < s.length() && s.charAt(i + 1) >= '0' && s.charAt(i + 1) <= '9') {
68+
i++;
69+
}
70+
TreeNode curr = new TreeNode(Integer.valueOf(s.substring(j, i + 1)));
71+
if (!stack.isEmpty()) {
72+
TreeNode parent = stack.peek();
73+
if (parent.left != null) {
74+
parent.right = curr;
75+
} else {
76+
parent.left = curr;
77+
}
78+
}
79+
stack.push(curr);
80+
}
4381
}
82+
return stack.isEmpty() ? null : stack.peek();
4483
}
45-
return cur;
4684
}
4785

4886
}

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,16 @@
99
import static junit.framework.Assert.assertEquals;
1010

1111
public class _536Test {
12-
private static _536 test;
12+
private static _536.Solution1 solution1;
13+
private static _536.Solution2 solution2;
1314
private static TreeNode expected;
1415
private static TreeNode actual;
1516
private static String s;
1617

1718
@BeforeClass
1819
public static void setup(){
19-
test = new _536();
20+
solution1 = new _536.Solution1();
21+
solution2 = new _536.Solution2();
2022
}
2123

2224
@Before
@@ -30,8 +32,10 @@ public void setupForEachTest(){
3032
public void test1(){
3133
s = "";
3234
expected = null;
33-
actual = test.str2tree(s);
35+
actual = solution1.str2tree(s);
3436
assertEquals(expected, actual);
3537

38+
actual = solution2.str2tree(s);
39+
assertEquals(expected, actual);
3640
}
3741
}

0 commit comments

Comments
 (0)