Skip to content

Commit 666b55c

Browse files
committed
feat: add solutions to lc problem: No.0606
No.0606.Construct String from Binary Tree
1 parent b73b470 commit 666b55c

File tree

4 files changed

+239
-0
lines changed

4 files changed

+239
-0
lines changed

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

+83
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,89 @@ func tree2str(root *TreeNode) string {
160160
}
161161
```
162162

163+
### **TypeScript**
164+
165+
```ts
166+
/**
167+
* Definition for a binary tree node.
168+
* class TreeNode {
169+
* val: number
170+
* left: TreeNode | null
171+
* right: TreeNode | null
172+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
173+
* this.val = (val===undefined ? 0 : val)
174+
* this.left = (left===undefined ? null : left)
175+
* this.right = (right===undefined ? null : right)
176+
* }
177+
* }
178+
*/
179+
180+
function tree2str(root: TreeNode | null): string {
181+
if (root == null) {
182+
return '';
183+
}
184+
if (root.left == null && root.right == null) {
185+
return `${root.val}`;
186+
}
187+
return `${root.val}(${root.left ? tree2str(root.left) : ''})${
188+
root.right ? `(${tree2str(root.right)})` : ''
189+
}`;
190+
}
191+
```
192+
193+
### **Rust**
194+
195+
```rust
196+
// Definition for a binary tree node.
197+
// #[derive(Debug, PartialEq, Eq)]
198+
// pub struct TreeNode {
199+
// pub val: i32,
200+
// pub left: Option<Rc<RefCell<TreeNode>>>,
201+
// pub right: Option<Rc<RefCell<TreeNode>>>,
202+
// }
203+
//
204+
// impl TreeNode {
205+
// #[inline]
206+
// pub fn new(val: i32) -> Self {
207+
// TreeNode {
208+
// val,
209+
// left: None,
210+
// right: None
211+
// }
212+
// }
213+
// }
214+
use std::cell::RefCell;
215+
use std::rc::Rc;
216+
impl Solution {
217+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut String) {
218+
if let Some(node) = root {
219+
let node = node.borrow();
220+
res.push_str(node.val.to_string().as_str());
221+
222+
if node.left.is_none() && node.right.is_none() {
223+
return;
224+
}
225+
res.push('(');
226+
if node.left.is_some() {
227+
Self::dfs(&node.left, res);
228+
}
229+
res.push(')');
230+
if node.right.is_some() {
231+
res.push('(');
232+
Self::dfs(&node.right, res);
233+
res.push(')');
234+
}
235+
}
236+
}
237+
238+
pub fn tree2str(root: Option<Rc<RefCell<TreeNode>>>) -> String {
239+
let mut res = String::new();
240+
Self::dfs(&root, &mut res);
241+
res
242+
}
243+
}
244+
```
245+
163246
### **...**
164247

165248
```

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

+83
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,89 @@ func tree2str(root *TreeNode) string {
144144
}
145145
```
146146

147+
### **TypeScript**
148+
149+
```ts
150+
/**
151+
* Definition for a binary tree node.
152+
* class TreeNode {
153+
* val: number
154+
* left: TreeNode | null
155+
* right: TreeNode | null
156+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
157+
* this.val = (val===undefined ? 0 : val)
158+
* this.left = (left===undefined ? null : left)
159+
* this.right = (right===undefined ? null : right)
160+
* }
161+
* }
162+
*/
163+
164+
function tree2str(root: TreeNode | null): string {
165+
if (root == null) {
166+
return '';
167+
}
168+
if (root.left == null && root.right == null) {
169+
return `${root.val}`;
170+
}
171+
return `${root.val}(${root.left ? tree2str(root.left) : ''})${
172+
root.right ? `(${tree2str(root.right)})` : ''
173+
}`;
174+
}
175+
```
176+
177+
### **Rust**
178+
179+
```rust
180+
// Definition for a binary tree node.
181+
// #[derive(Debug, PartialEq, Eq)]
182+
// pub struct TreeNode {
183+
// pub val: i32,
184+
// pub left: Option<Rc<RefCell<TreeNode>>>,
185+
// pub right: Option<Rc<RefCell<TreeNode>>>,
186+
// }
187+
//
188+
// impl TreeNode {
189+
// #[inline]
190+
// pub fn new(val: i32) -> Self {
191+
// TreeNode {
192+
// val,
193+
// left: None,
194+
// right: None
195+
// }
196+
// }
197+
// }
198+
use std::cell::RefCell;
199+
use std::rc::Rc;
200+
impl Solution {
201+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut String) {
202+
if let Some(node) = root {
203+
let node = node.borrow();
204+
res.push_str(node.val.to_string().as_str());
205+
206+
if node.left.is_none() && node.right.is_none() {
207+
return;
208+
}
209+
res.push('(');
210+
if node.left.is_some() {
211+
Self::dfs(&node.left, res);
212+
}
213+
res.push(')');
214+
if node.right.is_some() {
215+
res.push('(');
216+
Self::dfs(&node.right, res);
217+
res.push(')');
218+
}
219+
}
220+
}
221+
222+
pub fn tree2str(root: Option<Rc<RefCell<TreeNode>>>) -> String {
223+
let mut res = String::new();
224+
Self::dfs(&root, &mut res);
225+
res
226+
}
227+
}
228+
```
229+
147230
### **...**
148231

149232
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::cell::RefCell;
20+
use std::rc::Rc;
21+
impl Solution {
22+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, res: &mut String) {
23+
if let Some(node) = root {
24+
let node = node.borrow();
25+
res.push_str(node.val.to_string().as_str());
26+
27+
if node.left.is_none() && node.right.is_none() {
28+
return;
29+
}
30+
res.push('(');
31+
if node.left.is_some() {
32+
Self::dfs(&node.left, res);
33+
}
34+
res.push(')');
35+
if node.right.is_some() {
36+
res.push('(');
37+
Self::dfs(&node.right, res);
38+
res.push(')');
39+
}
40+
}
41+
}
42+
43+
pub fn tree2str(root: Option<Rc<RefCell<TreeNode>>>) -> String {
44+
let mut res = String::new();
45+
Self::dfs(&root, &mut res);
46+
res
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function tree2str(root: TreeNode | null): string {
16+
if (root == null) {
17+
return '';
18+
}
19+
if (root.left == null && root.right == null) {
20+
return `${root.val}`;
21+
}
22+
return `${root.val}(${root.left ? tree2str(root.left) : ''})${
23+
root.right ? `(${tree2str(root.right)})` : ''
24+
}`;
25+
}

0 commit comments

Comments
 (0)