Skip to content

Commit f03c190

Browse files
committed
feat: add solutions to lc problem: No.0655
No.0655.Print Binary Tree
1 parent 21ec9d5 commit f03c190

File tree

4 files changed

+347
-0
lines changed

4 files changed

+347
-0
lines changed

solution/0600-0699/0655.Print Binary Tree/README.md

+119
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,125 @@ func max(a, b int) int {
238238
}
239239
```
240240

241+
### **TypeScript**
242+
243+
```ts
244+
/**
245+
* Definition for a binary tree node.
246+
* class TreeNode {
247+
* val: number
248+
* left: TreeNode | null
249+
* right: TreeNode | null
250+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
251+
* this.val = (val===undefined ? 0 : val)
252+
* this.left = (left===undefined ? null : left)
253+
* this.right = (right===undefined ? null : right)
254+
* }
255+
* }
256+
*/
257+
258+
function printTree(root: TreeNode | null): string[][] {
259+
const getHeight = (root: TreeNode | null, h: number) => {
260+
if (root == null) {
261+
return h - 1;
262+
}
263+
return Math.max(
264+
getHeight(root.left, h + 1),
265+
getHeight(root.right, h + 1),
266+
);
267+
};
268+
269+
const height = getHeight(root, 0);
270+
const m = height + 1;
271+
const n = 2 ** (height + 1) - 1;
272+
const res: string[][] = Array.from({ length: m }, () =>
273+
new Array(n).fill(''),
274+
);
275+
const dfs = (root: TreeNode | null, i: number, j: number) => {
276+
if (root === null) {
277+
return;
278+
}
279+
const { val, left, right } = root;
280+
res[i][j] = val + '';
281+
dfs(left, i + 1, j - 2 ** (height - i - 1));
282+
dfs(right, i + 1, j + 2 ** (height - i - 1));
283+
};
284+
dfs(root, 0, (n - 1) >>> 1);
285+
return res;
286+
}
287+
```
288+
289+
### **Rust**
290+
291+
```rust
292+
// Definition for a binary tree node.
293+
// #[derive(Debug, PartialEq, Eq)]
294+
// pub struct TreeNode {
295+
// pub val: i32,
296+
// pub left: Option<Rc<RefCell<TreeNode>>>,
297+
// pub right: Option<Rc<RefCell<TreeNode>>>,
298+
// }
299+
//
300+
// impl TreeNode {
301+
// #[inline]
302+
// pub fn new(val: i32) -> Self {
303+
// TreeNode {
304+
// val,
305+
// left: None,
306+
// right: None
307+
// }
308+
// }
309+
// }
310+
use std::rc::Rc;
311+
use std::cell::RefCell;
312+
impl Solution {
313+
fn get_height(root: &Option<Rc<RefCell<TreeNode>>>, h: u32) -> u32 {
314+
if let Some(node) = root {
315+
let node = node.borrow();
316+
return Self::get_height(&node.left, h + 1).max(Self::get_height(&node.right, h + 1));
317+
}
318+
h - 1
319+
}
320+
321+
fn dfs(
322+
root: &Option<Rc<RefCell<TreeNode>>>,
323+
i: usize,
324+
j: usize,
325+
res: &mut Vec<Vec<String>>,
326+
height: u32,
327+
) {
328+
if root.is_none() {
329+
return;
330+
}
331+
let node = root.as_ref().unwrap().borrow();
332+
res[i][j] = node.val.to_string();
333+
Self::dfs(
334+
&node.left,
335+
i + 1,
336+
j - 2usize.pow(height - (i as u32) - 1),
337+
res,
338+
height,
339+
);
340+
Self::dfs(
341+
&node.right,
342+
i + 1,
343+
j + 2usize.pow(height - (i as u32) - 1),
344+
res,
345+
height,
346+
);
347+
}
348+
349+
pub fn print_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<String>> {
350+
let height = Self::get_height(&root, 0);
351+
let m = (height + 1) as usize;
352+
let n = 2usize.pow(height + 1) - 1;
353+
let mut res = vec![vec![String::new(); n]; m];
354+
Self::dfs(&root, 0, (n - 1) >> 1, &mut res, height);
355+
res
356+
}
357+
}
358+
```
359+
241360
### **...**
242361

243362
```

solution/0600-0699/0655.Print Binary Tree/README_EN.md

+119
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,125 @@ func max(a, b int) int {
220220
}
221221
```
222222

223+
### **TypeScript**
224+
225+
```ts
226+
/**
227+
* Definition for a binary tree node.
228+
* class TreeNode {
229+
* val: number
230+
* left: TreeNode | null
231+
* right: TreeNode | null
232+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
233+
* this.val = (val===undefined ? 0 : val)
234+
* this.left = (left===undefined ? null : left)
235+
* this.right = (right===undefined ? null : right)
236+
* }
237+
* }
238+
*/
239+
240+
function printTree(root: TreeNode | null): string[][] {
241+
const getHeight = (root: TreeNode | null, h: number) => {
242+
if (root == null) {
243+
return h - 1;
244+
}
245+
return Math.max(
246+
getHeight(root.left, h + 1),
247+
getHeight(root.right, h + 1),
248+
);
249+
};
250+
251+
const height = getHeight(root, 0);
252+
const m = height + 1;
253+
const n = 2 ** (height + 1) - 1;
254+
const res: string[][] = Array.from({ length: m }, () =>
255+
new Array(n).fill(''),
256+
);
257+
const dfs = (root: TreeNode | null, i: number, j: number) => {
258+
if (root === null) {
259+
return;
260+
}
261+
const { val, left, right } = root;
262+
res[i][j] = val + '';
263+
dfs(left, i + 1, j - 2 ** (height - i - 1));
264+
dfs(right, i + 1, j + 2 ** (height - i - 1));
265+
};
266+
dfs(root, 0, (n - 1) >>> 1);
267+
return res;
268+
}
269+
```
270+
271+
### **Rust**
272+
273+
```rust
274+
// Definition for a binary tree node.
275+
// #[derive(Debug, PartialEq, Eq)]
276+
// pub struct TreeNode {
277+
// pub val: i32,
278+
// pub left: Option<Rc<RefCell<TreeNode>>>,
279+
// pub right: Option<Rc<RefCell<TreeNode>>>,
280+
// }
281+
//
282+
// impl TreeNode {
283+
// #[inline]
284+
// pub fn new(val: i32) -> Self {
285+
// TreeNode {
286+
// val,
287+
// left: None,
288+
// right: None
289+
// }
290+
// }
291+
// }
292+
use std::rc::Rc;
293+
use std::cell::RefCell;
294+
impl Solution {
295+
fn get_height(root: &Option<Rc<RefCell<TreeNode>>>, h: u32) -> u32 {
296+
if let Some(node) = root {
297+
let node = node.borrow();
298+
return Self::get_height(&node.left, h + 1).max(Self::get_height(&node.right, h + 1));
299+
}
300+
h - 1
301+
}
302+
303+
fn dfs(
304+
root: &Option<Rc<RefCell<TreeNode>>>,
305+
i: usize,
306+
j: usize,
307+
res: &mut Vec<Vec<String>>,
308+
height: u32,
309+
) {
310+
if root.is_none() {
311+
return;
312+
}
313+
let node = root.as_ref().unwrap().borrow();
314+
res[i][j] = node.val.to_string();
315+
Self::dfs(
316+
&node.left,
317+
i + 1,
318+
j - 2usize.pow(height - (i as u32) - 1),
319+
res,
320+
height,
321+
);
322+
Self::dfs(
323+
&node.right,
324+
i + 1,
325+
j + 2usize.pow(height - (i as u32) - 1),
326+
res,
327+
height,
328+
);
329+
}
330+
331+
pub fn print_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<String>> {
332+
let height = Self::get_height(&root, 0);
333+
let m = (height + 1) as usize;
334+
let n = 2usize.pow(height + 1) - 1;
335+
let mut res = vec![vec![String::new(); n]; m];
336+
Self::dfs(&root, 0, (n - 1) >> 1, &mut res, height);
337+
res
338+
}
339+
}
340+
```
341+
223342
### **...**
224343

225344
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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::rc::Rc;
20+
use std::cell::RefCell;
21+
impl Solution {
22+
fn get_height(root: &Option<Rc<RefCell<TreeNode>>>, h: u32) -> u32 {
23+
if let Some(node) = root {
24+
let node = node.borrow();
25+
return Self::get_height(&node.left, h + 1).max(Self::get_height(&node.right, h + 1));
26+
}
27+
h - 1
28+
}
29+
30+
fn dfs(
31+
root: &Option<Rc<RefCell<TreeNode>>>,
32+
i: usize,
33+
j: usize,
34+
res: &mut Vec<Vec<String>>,
35+
height: u32,
36+
) {
37+
if root.is_none() {
38+
return;
39+
}
40+
let node = root.as_ref().unwrap().borrow();
41+
res[i][j] = node.val.to_string();
42+
Self::dfs(
43+
&node.left,
44+
i + 1,
45+
j - 2usize.pow(height - (i as u32) - 1),
46+
res,
47+
height,
48+
);
49+
Self::dfs(
50+
&node.right,
51+
i + 1,
52+
j + 2usize.pow(height - (i as u32) - 1),
53+
res,
54+
height,
55+
);
56+
}
57+
58+
pub fn print_tree(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<Vec<String>> {
59+
let height = Self::get_height(&root, 0);
60+
let m = (height + 1) as usize;
61+
let n = 2usize.pow(height + 1) - 1;
62+
let mut res = vec![vec![String::new(); n]; m];
63+
Self::dfs(&root, 0, (n - 1) >> 1, &mut res, height);
64+
res
65+
}
66+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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 printTree(root: TreeNode | null): string[][] {
16+
const getHeight = (root: TreeNode | null, h: number) => {
17+
if (root == null) {
18+
return h - 1;
19+
}
20+
return Math.max(
21+
getHeight(root.left, h + 1),
22+
getHeight(root.right, h + 1),
23+
);
24+
};
25+
26+
const height = getHeight(root, 0);
27+
const m = height + 1;
28+
const n = 2 ** (height + 1) - 1;
29+
const res: string[][] = Array.from({ length: m }, () =>
30+
new Array(n).fill(''),
31+
);
32+
const dfs = (root: TreeNode | null, i: number, j: number) => {
33+
if (root === null) {
34+
return;
35+
}
36+
const { val, left, right } = root;
37+
res[i][j] = val + '';
38+
dfs(left, i + 1, j - 2 ** (height - i - 1));
39+
dfs(right, i + 1, j + 2 ** (height - i - 1));
40+
};
41+
dfs(root, 0, (n - 1) >>> 1);
42+
return res;
43+
}

0 commit comments

Comments
 (0)