Skip to content

Commit 9241d53

Browse files
committed
feat: add solutions to lc problem: No.0606
No.0606.Construct String from Binary Tree
1 parent 4e5e9ba commit 9241d53

File tree

6 files changed

+273
-10
lines changed

6 files changed

+273
-10
lines changed

solution/0600-0699/0606.Construct String from Binary Tree/README.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,110 @@
5454
<!-- 这里可写当前语言的特殊实现逻辑 -->
5555

5656
```python
57-
57+
# Definition for a binary tree node.
58+
# class TreeNode:
59+
# def __init__(self, val=0, left=None, right=None):
60+
# self.val = val
61+
# self.left = left
62+
# self.right = right
63+
class Solution:
64+
def tree2str(self, root: Optional[TreeNode]) -> str:
65+
def dfs(root):
66+
if root is None:
67+
return ''
68+
if root.left is None and root.right is None:
69+
return str(root.val)
70+
if root.right is None:
71+
return f'{root.val}({dfs(root.left)})'
72+
return f'{root.val}({dfs(root.left)})({dfs(root.right)})'
73+
74+
return dfs(root)
5875
```
5976

6077
### **Java**
6178

6279
<!-- 这里可写当前语言的特殊实现逻辑 -->
6380

6481
```java
82+
/**
83+
* Definition for a binary tree node.
84+
* public class TreeNode {
85+
* int val;
86+
* TreeNode left;
87+
* TreeNode right;
88+
* TreeNode() {}
89+
* TreeNode(int val) { this.val = val; }
90+
* TreeNode(int val, TreeNode left, TreeNode right) {
91+
* this.val = val;
92+
* this.left = left;
93+
* this.right = right;
94+
* }
95+
* }
96+
*/
97+
class Solution {
98+
public String tree2str(TreeNode root) {
99+
if (root == null) {
100+
return "";
101+
}
102+
if (root.left == null && root.right == null) {
103+
return root.val + "";
104+
}
105+
if (root.right == null) {
106+
return root.val + "(" + tree2str(root.left) + ")";
107+
}
108+
return root.val + "(" + tree2str(root.left) + ")(" + tree2str(root.right) + ")";
109+
}
110+
}
111+
```
112+
113+
### **C++**
114+
115+
```cpp
116+
/**
117+
* Definition for a binary tree node.
118+
* struct TreeNode {
119+
* int val;
120+
* TreeNode *left;
121+
* TreeNode *right;
122+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
123+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
124+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
125+
* };
126+
*/
127+
class Solution {
128+
public:
129+
string tree2str(TreeNode* root) {
130+
if (!root) return "";
131+
if (!root->left && !root->right) return to_string(root->val);
132+
if (!root->right) return to_string(root->val) + "(" + tree2str(root->left) + ")";
133+
return to_string(root->val) + "(" + tree2str(root->left) + ")(" + tree2str(root->right) + ")";
134+
}
135+
};
136+
```
65137
138+
### **Go**
139+
140+
```go
141+
/**
142+
* Definition for a binary tree node.
143+
* type TreeNode struct {
144+
* Val int
145+
* Left *TreeNode
146+
* Right *TreeNode
147+
* }
148+
*/
149+
func tree2str(root *TreeNode) string {
150+
if root == nil {
151+
return ""
152+
}
153+
if root.Left == nil && root.Right == nil {
154+
return strconv.Itoa(root.Val)
155+
}
156+
if root.Right == nil {
157+
return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")"
158+
}
159+
return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")(" + tree2str(root.Right) + ")"
160+
}
66161
```
67162

68163
### **...**

solution/0600-0699/0606.Construct String from Binary Tree/README_EN.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,108 @@
6767
### **Python3**
6868

6969
```python
70-
70+
# Definition for a binary tree node.
71+
# class TreeNode:
72+
# def __init__(self, val=0, left=None, right=None):
73+
# self.val = val
74+
# self.left = left
75+
# self.right = right
76+
class Solution:
77+
def tree2str(self, root: Optional[TreeNode]) -> str:
78+
def dfs(root):
79+
if root is None:
80+
return ''
81+
if root.left is None and root.right is None:
82+
return str(root.val)
83+
if root.right is None:
84+
return f'{root.val}({dfs(root.left)})'
85+
return f'{root.val}({dfs(root.left)})({dfs(root.right)})'
86+
87+
return dfs(root)
7188
```
7289

7390
### **Java**
7491

7592
```java
93+
/**
94+
* Definition for a binary tree node.
95+
* public class TreeNode {
96+
* int val;
97+
* TreeNode left;
98+
* TreeNode right;
99+
* TreeNode() {}
100+
* TreeNode(int val) { this.val = val; }
101+
* TreeNode(int val, TreeNode left, TreeNode right) {
102+
* this.val = val;
103+
* this.left = left;
104+
* this.right = right;
105+
* }
106+
* }
107+
*/
108+
class Solution {
109+
public String tree2str(TreeNode root) {
110+
if (root == null) {
111+
return "";
112+
}
113+
if (root.left == null && root.right == null) {
114+
return root.val + "";
115+
}
116+
if (root.right == null) {
117+
return root.val + "(" + tree2str(root.left) + ")";
118+
}
119+
return root.val + "(" + tree2str(root.left) + ")(" + tree2str(root.right) + ")";
120+
}
121+
}
122+
```
123+
124+
### **C++**
125+
126+
```cpp
127+
/**
128+
* Definition for a binary tree node.
129+
* struct TreeNode {
130+
* int val;
131+
* TreeNode *left;
132+
* TreeNode *right;
133+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
135+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
136+
* };
137+
*/
138+
class Solution {
139+
public:
140+
string tree2str(TreeNode* root) {
141+
if (!root) return "";
142+
if (!root->left && !root->right) return to_string(root->val);
143+
if (!root->right) return to_string(root->val) + "(" + tree2str(root->left) + ")";
144+
return to_string(root->val) + "(" + tree2str(root->left) + ")(" + tree2str(root->right) + ")";
145+
}
146+
};
147+
```
76148
149+
### **Go**
150+
151+
```go
152+
/**
153+
* Definition for a binary tree node.
154+
* type TreeNode struct {
155+
* Val int
156+
* Left *TreeNode
157+
* Right *TreeNode
158+
* }
159+
*/
160+
func tree2str(root *TreeNode) string {
161+
if root == nil {
162+
return ""
163+
}
164+
if root.Left == nil && root.Right == nil {
165+
return strconv.Itoa(root.Val)
166+
}
167+
if root.Right == nil {
168+
return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")"
169+
}
170+
return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")(" + tree2str(root.Right) + ")"
171+
}
77172
```
78173

79174
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
string tree2str(TreeNode* root) {
15+
if (!root) return "";
16+
if (!root->left && !root->right) return to_string(root->val);
17+
if (!root->right) return to_string(root->val) + "(" + tree2str(root->left) + ")";
18+
return to_string(root->val) + "(" + tree2str(root->left) + ")(" + tree2str(root->right) + ")";
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func tree2str(root *TreeNode) string {
10+
if root == nil {
11+
return ""
12+
}
13+
if root.Left == nil && root.Right == nil {
14+
return strconv.Itoa(root.Val)
15+
}
16+
if root.Right == nil {
17+
return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")"
18+
}
19+
return strconv.Itoa(root.Val) + "(" + tree2str(root.Left) + ")(" + tree2str(root.Right) + ")"
20+
}
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,29 @@
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+
*/
116
class Solution {
2-
public String tree2str(TreeNode t) {
3-
if (t == null) {
17+
public String tree2str(TreeNode root) {
18+
if (root == null) {
419
return "";
520
}
6-
if (t.right != null) {
7-
return t.val + "(" + tree2str(t.left) + ")" + "(" + tree2str(t.right) + ")";
21+
if (root.left == null && root.right == null) {
22+
return root.val + "";
823
}
9-
if (t.left != null) {
10-
return t.val + "(" + tree2str(t.left) + ")";
24+
if (root.right == null) {
25+
return root.val + "(" + tree2str(root.left) + ")";
1126
}
12-
return t.val + "";
27+
return root.val + "(" + tree2str(root.left) + ")(" + tree2str(root.right) + ")";
1328
}
14-
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Definition for a binary tree node.
2+
# class TreeNode:
3+
# def __init__(self, val=0, left=None, right=None):
4+
# self.val = val
5+
# self.left = left
6+
# self.right = right
7+
class Solution:
8+
def tree2str(self, root: Optional[TreeNode]) -> str:
9+
def dfs(root):
10+
if root is None:
11+
return ''
12+
if root.left is None and root.right is None:
13+
return str(root.val)
14+
if root.right is None:
15+
return f'{root.val}({dfs(root.left)})'
16+
return f'{root.val}({dfs(root.left)})({dfs(root.right)})'
17+
18+
return dfs(root)

0 commit comments

Comments
 (0)