Skip to content

Commit bfdd40a

Browse files
add 606
1 parent 3a6eb6e commit bfdd40a

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

README.md

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

2121
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2222
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
23+
|606|[Construct String from Binary Tree](https://leetcode.com/problems/construct-string-from-binary-tree/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_606.java) | O(n) |O(n) | Easy | Tree, Recursion
2324
|600|[Non-negative Integers without Consecutive Ones](https://leetcode.com/problems/non-negative-integers-without-consecutive-ones/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_600.java) | O(log2(max_int) = 32) | O(log2(max_int) = 32) | Hard | Bit Manipulation, DP
2425
|599|[Minimum Index Sum of Two Lists](https://leetcode.com/problems/minimum-index-sum-of-two-lists/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_599.java) | O(max(m,n))|O(max(m,n)) | Easy | HashMap
2526
|598|[Range Addition II](https://leetcode.com/problems/range-addition-ii/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_598.java) | O(n) (n is the number of operations) |O(1) | Easy |
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.fishercoder.solutions;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
5+
/**
6+
* 606. Construct String from Binary Tree
7+
*
8+
* You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.
9+
The null node needs to be represented by empty parenthesis pair "()".
10+
And you need to omit all the empty parenthesis pairs that don't affect the one-to-one mapping relationship between the string
11+
and the original binary tree.
12+
13+
Example 1:
14+
Input: Binary tree: [1,2,3,4]
15+
1
16+
/ \
17+
2 3
18+
/
19+
4
20+
21+
Output: "1(2(4))(3)"
22+
23+
Explanation: Originallay it needs to be "1(2(4)())(3()())",
24+
but you need to omit all the unnecessary empty parenthesis pairs.
25+
And it will be "1(2(4))(3)".
26+
27+
Example 2:
28+
Input: Binary tree: [1,2,3,null,4]
29+
1
30+
/ \
31+
2 3
32+
\
33+
4
34+
35+
Output: "1(2()(4))(3)"
36+
37+
Explanation: Almost the same as the first example,
38+
except we can't omit the first parenthesis pair to break the one-to-one mapping relationship between the input and the output.
39+
*/
40+
public class _606 {
41+
42+
public String tree2str(TreeNode t) {
43+
if (t == null) return "";
44+
String result = "" + t.val;
45+
String left = tree2str(t.left);
46+
String right = tree2str(t.right);
47+
if (left.equals("") && right.equals("")) return result;
48+
if (left.equals("")) return result + "()(" + right + ")";
49+
if (right.equals("")) return result + "(" + left + ")";
50+
return result + "(" + left + ")(" + right + ")";
51+
}
52+
53+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.common.classes.TreeNode;
4+
import com.fishercoder.solutions._606;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
/**
11+
* Created by stevesun on 6/4/17.
12+
*/
13+
public class _606Test {
14+
private static _606 test;
15+
private static TreeNode t;
16+
17+
@BeforeClass
18+
public static void setup(){
19+
test = new _606();
20+
}
21+
22+
@Test
23+
public void test1(){
24+
t = new TreeNode(1);
25+
t.left = new TreeNode(2);
26+
t.right = new TreeNode(3);
27+
t.left.left = new TreeNode(4);
28+
System.out.println("Test1");
29+
assertEquals("1(2(4))(3)", test.tree2str(t));
30+
}
31+
32+
@Test
33+
public void test2(){
34+
t = new TreeNode(1);
35+
t.left = new TreeNode(2);
36+
t.right = new TreeNode(3);
37+
t.left.right = new TreeNode(4);
38+
System.out.println("Test2");
39+
assertEquals("1(2()(4))(3)", test.tree2str(t));
40+
}
41+
}

0 commit comments

Comments
 (0)