Skip to content

Commit 304b030

Browse files
Time: 38 ms (22.20%), Space: 54.4 MB (47.91%) - LeetHub
1 parent 4317662 commit 304b030

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
public String tree2str(TreeNode root) {
18+
if(root == null)
19+
return "";
20+
21+
if(root.left == null && root.right == null)
22+
return String.valueOf(root.val);
23+
24+
String l = tree2str(root.left);
25+
String r = tree2str(root.right);
26+
27+
if(r == "")
28+
return String.valueOf(root.val) + "("+l+")";
29+
else
30+
return String.valueOf(root.val) + "("+ l + ")" + "("+ r +")";
31+
}
32+
}

0 commit comments

Comments
 (0)