- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNo606.java
58 lines (46 loc) · 1002 Bytes
/
No606.java
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
packageAlgorithm.leetcode.String;
/**
* @author tujietg
* @date 2019/9/10 10:02 AM
*/
classTreeNode {
intval;
TreeNodeleft;
TreeNoderight;
TreeNode(intx) {
val = x;
}
}
publicclassNo606 {
publicstaticvoidmain(String[] args) {
TreeNodet = newTreeNode(1);
TreeNodet1 = newTreeNode(2);
TreeNodet2 = newTreeNode(3);
TreeNodet3 = newTreeNode(4);
t.left = t1;
t.right = t2;
t.left.left = t3;
Stringresult = Solution.tree2str(t);
// System.out.println(result);
}
publicstaticclassSolution {
publicstaticStringtree2str(TreeNodet) {
if (t == null) {
return"";
}
Stringresult = t.val + "";
Stringleft = tree2str(t.left);
Stringright = tree2str(t.right);
if (left == "" && right == "") {
returnresult;
}
if (left == "") {
returnresult + "()" + "(" + right + ")";
}
if (right == "") {
returnresult + "(" + left + ")";
}
returnresult + "(" + left + ")" + "(" + right + ")";
}
}
}