Skip to content

Commit 4e5e9ba

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

File tree

6 files changed

+513
-2
lines changed

6 files changed

+513
-2
lines changed

solution/0500-0599/0536.Construct Binary Tree from String/README.md

+176-1
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,197 @@
4141

4242
<!-- 这里可写通用的实现逻辑 -->
4343

44+
DFS。
45+
46+
利用 cnt 变量,检测子树的位置,若 cnt == 0,说明已经定位到其中一棵子树,start 表示子树开始的位置(注意要去掉括号)。
47+
4448
<!-- tabs:start -->
4549

4650
### **Python3**
4751

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

5054
```python
51-
55+
# Definition for a binary tree node.
56+
# class TreeNode:
57+
# def __init__(self, val=0, left=None, right=None):
58+
# self.val = val
59+
# self.left = left
60+
# self.right = right
61+
class Solution:
62+
def str2tree(self, s: str) -> TreeNode:
63+
def dfs(s):
64+
if not s:
65+
return None
66+
p = s.find('(')
67+
if p == -1:
68+
return TreeNode(int(s))
69+
root = TreeNode(int(s[:p]))
70+
start = p
71+
cnt = 0
72+
for i in range(p, len(s)):
73+
if s[i] == '(':
74+
cnt += 1
75+
elif s[i] == ')':
76+
cnt -= 1
77+
if cnt == 0:
78+
if start == p:
79+
root.left = dfs(s[start + 1: i])
80+
start = i + 1
81+
else:
82+
root.right = dfs(s[start + 1: i])
83+
return root
84+
85+
return dfs(s)
5286
```
5387

5488
### **Java**
5589

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

5892
```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 TreeNode str2tree(String s) {
110+
return dfs(s);
111+
}
112+
113+
private TreeNode dfs(String s) {
114+
if ("".equals(s)) {
115+
return null;
116+
}
117+
int p = s.indexOf("(");
118+
if (p == -1) {
119+
return new TreeNode(Integer.parseInt(s));
120+
}
121+
TreeNode root = new TreeNode(Integer.parseInt(s.substring(0, p)));
122+
int start = p;
123+
int cnt = 0;
124+
for (int i = p; i < s.length(); ++i) {
125+
if (s.charAt(i) == '(') {
126+
++cnt;
127+
} else if (s.charAt(i) == ')') {
128+
--cnt;
129+
}
130+
if (cnt == 0) {
131+
if (start == p) {
132+
root.left = dfs(s.substring(start + 1, i));
133+
start = i + 1;
134+
} else {
135+
root.right = dfs(s.substring(start + 1, i));
136+
}
137+
}
138+
}
139+
return root;
140+
}
141+
}
142+
```
143+
144+
### **C++**
145+
146+
```cpp
147+
/**
148+
* Definition for a binary tree node.
149+
* struct TreeNode {
150+
* int val;
151+
* TreeNode *left;
152+
* TreeNode *right;
153+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
154+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
155+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
156+
* };
157+
*/
158+
class Solution {
159+
public:
160+
TreeNode* str2tree(string s) {
161+
return dfs(s);
162+
}
163+
164+
TreeNode* dfs(string s) {
165+
if (s == "") return nullptr;
166+
int p = s.find("(");
167+
if (p == s.npos) return new TreeNode(stoi(s));
168+
TreeNode* root = new TreeNode(stoi(s.substr(0, p)));
169+
int start = p;
170+
int cnt = 0;
171+
for (int i = p; i < s.size(); ++i)
172+
{
173+
if (s[i] == '(') ++cnt;
174+
else if (s[i] == ')') --cnt;
175+
if (cnt == 0)
176+
{
177+
if (start == p)
178+
{
179+
root->left = dfs(s.substr(start + 1, i - start - 1));
180+
start = i + 1;
181+
}
182+
else root->right = dfs(s.substr(start + 1, i - start - 1));
183+
}
184+
}
185+
return root;
186+
}
187+
};
188+
```
59189

190+
### **Go**
191+
192+
```go
193+
/**
194+
* Definition for a binary tree node.
195+
* type TreeNode struct {
196+
* Val int
197+
* Left *TreeNode
198+
* Right *TreeNode
199+
* }
200+
*/
201+
func str2tree(s string) *TreeNode {
202+
var dfs func(s string) *TreeNode
203+
dfs = func(s string) *TreeNode {
204+
if s == "" {
205+
return nil
206+
}
207+
p := strings.IndexAny(s, "(")
208+
if p == -1 {
209+
v, _ := strconv.Atoi(s)
210+
return &TreeNode{Val: v}
211+
}
212+
v, _ := strconv.Atoi(s[:p])
213+
root := &TreeNode{Val: v}
214+
start := p
215+
cnt := 0
216+
for i := p; i < len(s); i++ {
217+
if s[i] == '(' {
218+
cnt++
219+
} else if s[i] == ')' {
220+
cnt--
221+
}
222+
if cnt == 0 {
223+
if p == start {
224+
root.Left = dfs(s[start+1 : i])
225+
start = i + 1
226+
} else {
227+
root.Right = dfs(s[start+1 : i])
228+
}
229+
}
230+
}
231+
return root
232+
}
233+
return dfs(s)
234+
}
60235
```
61236

62237
### **...**

solution/0500-0599/0536.Construct Binary Tree from String/README_EN.md

+174-1
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,191 @@
4242

4343
## Solutions
4444

45+
DFS.
46+
4547
<!-- tabs:start -->
4648

4749
### **Python3**
4850

4951
```python
50-
52+
# Definition for a binary tree node.
53+
# class TreeNode:
54+
# def __init__(self, val=0, left=None, right=None):
55+
# self.val = val
56+
# self.left = left
57+
# self.right = right
58+
class Solution:
59+
def str2tree(self, s: str) -> TreeNode:
60+
def dfs(s):
61+
if not s:
62+
return None
63+
p = s.find('(')
64+
if p == -1:
65+
return TreeNode(int(s))
66+
root = TreeNode(int(s[:p]))
67+
start = p
68+
cnt = 0
69+
for i in range(p, len(s)):
70+
if s[i] == '(':
71+
cnt += 1
72+
elif s[i] == ')':
73+
cnt -= 1
74+
if cnt == 0:
75+
if start == p:
76+
root.left = dfs(s[start + 1: i])
77+
start = i + 1
78+
else:
79+
root.right = dfs(s[start + 1: i])
80+
return root
81+
82+
return dfs(s)
5183
```
5284

5385
### **Java**
5486

5587
```java
88+
/**
89+
* Definition for a binary tree node.
90+
* public class TreeNode {
91+
* int val;
92+
* TreeNode left;
93+
* TreeNode right;
94+
* TreeNode() {}
95+
* TreeNode(int val) { this.val = val; }
96+
* TreeNode(int val, TreeNode left, TreeNode right) {
97+
* this.val = val;
98+
* this.left = left;
99+
* this.right = right;
100+
* }
101+
* }
102+
*/
103+
class Solution {
104+
public TreeNode str2tree(String s) {
105+
return dfs(s);
106+
}
107+
108+
private TreeNode dfs(String s) {
109+
if ("".equals(s)) {
110+
return null;
111+
}
112+
int p = s.indexOf("(");
113+
if (p == -1) {
114+
return new TreeNode(Integer.parseInt(s));
115+
}
116+
TreeNode root = new TreeNode(Integer.parseInt(s.substring(0, p)));
117+
int start = p;
118+
int cnt = 0;
119+
for (int i = p; i < s.length(); ++i) {
120+
if (s.charAt(i) == '(') {
121+
++cnt;
122+
} else if (s.charAt(i) == ')') {
123+
--cnt;
124+
}
125+
if (cnt == 0) {
126+
if (start == p) {
127+
root.left = dfs(s.substring(start + 1, i));
128+
start = i + 1;
129+
} else {
130+
root.right = dfs(s.substring(start + 1, i));
131+
}
132+
}
133+
}
134+
return root;
135+
}
136+
}
137+
```
138+
139+
### **C++**
140+
141+
```cpp
142+
/**
143+
* Definition for a binary tree node.
144+
* struct TreeNode {
145+
* int val;
146+
* TreeNode *left;
147+
* TreeNode *right;
148+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
149+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
150+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
151+
* };
152+
*/
153+
class Solution {
154+
public:
155+
TreeNode* str2tree(string s) {
156+
return dfs(s);
157+
}
158+
159+
TreeNode* dfs(string s) {
160+
if (s == "") return nullptr;
161+
int p = s.find("(");
162+
if (p == s.npos) return new TreeNode(stoi(s));
163+
TreeNode* root = new TreeNode(stoi(s.substr(0, p)));
164+
int start = p;
165+
int cnt = 0;
166+
for (int i = p; i < s.size(); ++i)
167+
{
168+
if (s[i] == '(') ++cnt;
169+
else if (s[i] == ')') --cnt;
170+
if (cnt == 0)
171+
{
172+
if (start == p)
173+
{
174+
root->left = dfs(s.substr(start + 1, i - start - 1));
175+
start = i + 1;
176+
}
177+
else root->right = dfs(s.substr(start + 1, i - start - 1));
178+
}
179+
}
180+
return root;
181+
}
182+
};
183+
```
56184

185+
### **Go**
186+
187+
```go
188+
/**
189+
* Definition for a binary tree node.
190+
* type TreeNode struct {
191+
* Val int
192+
* Left *TreeNode
193+
* Right *TreeNode
194+
* }
195+
*/
196+
func str2tree(s string) *TreeNode {
197+
var dfs func(s string) *TreeNode
198+
dfs = func(s string) *TreeNode {
199+
if s == "" {
200+
return nil
201+
}
202+
p := strings.IndexAny(s, "(")
203+
if p == -1 {
204+
v, _ := strconv.Atoi(s)
205+
return &TreeNode{Val: v}
206+
}
207+
v, _ := strconv.Atoi(s[:p])
208+
root := &TreeNode{Val: v}
209+
start := p
210+
cnt := 0
211+
for i := p; i < len(s); i++ {
212+
if s[i] == '(' {
213+
cnt++
214+
} else if s[i] == ')' {
215+
cnt--
216+
}
217+
if cnt == 0 {
218+
if p == start {
219+
root.Left = dfs(s[start+1 : i])
220+
start = i + 1
221+
} else {
222+
root.Right = dfs(s[start+1 : i])
223+
}
224+
}
225+
}
226+
return root
227+
}
228+
return dfs(s)
229+
}
57230
```
58231

59232
### **...**

0 commit comments

Comments
 (0)