Skip to content

Commit c57b3cc

Browse files
committed
q85
1 parent fdad293 commit c57b3cc

File tree

3 files changed

+150
-18
lines changed

3 files changed

+150
-18
lines changed

src/tree/mod.rs

Lines changed: 66 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,80 @@ impl TreeNode {
2222
}
2323
}
2424

25-
fn str(&self, mut lvl: usize) -> String {
26-
let mut result = format!("|{}{}\x0a", "__".repeat(lvl), self.val);
27-
lvl+=1;
28-
match (&self.left, &self.right) {
29-
(Some(l), Some(r)) => {
30-
result.push_str(&(**l).borrow().str(lvl));
31-
result.push_str(&(**r).borrow().str(lvl));
32-
},
33-
(None, Some(r)) => {
34-
result.push_str(&format!("|{}null\x0a", "__".repeat(lvl)));
35-
result.push_str(&(**r).borrow().str(lvl));
25+
pub fn str(&self) -> String {
26+
let (lines, _, _, _) = self.draw_aux();
27+
lines.join("\n")
28+
}
29+
30+
pub fn draw(&self) {
31+
let (lines, _, _, _) = self.draw_aux();
32+
lines.iter().map(|x| println!("{}", x)).collect::<()>();
33+
}
34+
35+
fn draw_aux(&self) -> (Vec<String>, usize, usize, usize){
36+
return match (&self.left, &self.right) {
37+
(None, None) => {
38+
let line = format!("{}", self.val);
39+
let width = line.len();
40+
let height = 1;
41+
let mid = width / 2;
42+
(vec!(line), width, height, mid)
3643
},
3744
(Some(l), None) => {
38-
result.push_str(&(**l).borrow().str(lvl));
39-
result.push_str(&format!("|{}null\x0a", "__".repeat(lvl)));
45+
let (lines, n, p, x) = (**l).borrow().draw_aux();
46+
let s = format!("{}", self.val);
47+
let u = s.len();
48+
let first_line = " ".repeat(x + 1) + &"_".repeat(n - x - 1) + &s;
49+
let second_line = " ".repeat(x) + "/" + &" ".repeat(n - x - 1 + u);
50+
let mut shifted_lines = lines.iter().map(|x| x.to_owned() + &" ".repeat(u)).collect::<Vec<String>>();
51+
shifted_lines.insert(0, first_line);
52+
shifted_lines.insert(1, second_line);
53+
(shifted_lines, n + u, p + 2, n + u / 2)
4054
},
41-
(None, None) => ()
55+
56+
(None, Some(r)) => {
57+
let (lines, n, p, x) = (**r).borrow().draw_aux();
58+
let s = format!("{}", self.val);
59+
let u = s.len();
60+
let first_line = s + &"_".repeat(x) + &" ".repeat(n - x);
61+
let second_line = " ".repeat(u + x) + "\\" + &" ".repeat(n - x - 1);
62+
let mut shifted_lines = lines.iter().map(|x| x.to_owned() + &" ".repeat(u)).collect::<Vec<String>>();
63+
shifted_lines.insert(0, first_line);
64+
shifted_lines.insert(1, second_line);
65+
(shifted_lines, n + u, p + 2, n + u / 2)
66+
},
67+
(Some(l), Some(r)) => {
68+
let (mut lefts, n, p, x) = (**l).borrow().draw_aux();
69+
let (mut rights, m, q, y) = (**r).borrow().draw_aux();
70+
let s = format!("{}", self.val);
71+
let u = s.len();
72+
let first_line = " ".repeat(x + 1) + &"_".repeat(n - x - 1) + &s + &"_".repeat(y) + &" ".repeat(m - y);
73+
let second_line = " ".repeat(x) + "/" + &" ".repeat(n - x - 1 + u + y) + "\\" + &" ".repeat(m - y - 1);
74+
if p < q {
75+
for _ in 0..(q - p) {
76+
lefts.push(" ".repeat(n))
77+
}
78+
} else if q < p {
79+
for _ in 0..(p - q) {
80+
rights.push(" ".repeat(n))
81+
}
82+
}
83+
84+
let mut shifted_lines: Vec<String> = lefts.iter().zip(rights.iter()).map(|(x, y)| x.to_owned() + &" ".repeat(u) + y).collect();
85+
shifted_lines.insert(0, first_line);
86+
shifted_lines.insert(1, second_line);
87+
(shifted_lines, n + m + u, if p > q { p + 2 } else { q + 2 }, n + u / 2)
88+
}
4289
}
43-
result
90+
4491
}
4592
}
4693

4794

4895
impl Display for TreeNode {
4996

5097
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
51-
write!(f, "{}", self.str(0))
98+
write!(f, "{}", self.str())
5299
}
53100
}
54101

@@ -90,4 +137,6 @@ macro_rules! tree {
90137
}
91138

92139

93-
mod q108;
140+
mod q108;
141+
mod q95;
142+
mod q129;

src/tree/q108.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,11 @@ mod tests {
7474

7575
#[test]
7676
fn test_1() {
77-
let nums = vec!(-10,-3,0,5,9);
77+
let nums = vec!(-10, -3, 0, 5, 9);
7878
let tree = Solution::sorted_array_to_bst(nums).unwrap();
79+
7980
println!("{}", (*tree).borrow());
81+
8082
assert_eq!(
8183
Solution::sorted_array_to_bst(vec![-10, -3, 0, 5, 9]),
8284
tree![0, -3, 9, -10, null, 5]

src/tree/q95.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//! Given an integer n, generate all structurally unique BST's (binary search trees) that store values 1 ... n.
2+
//!
3+
//! Example:
4+
//! ```shell
5+
//! Input: 3
6+
//! Output:
7+
//! [
8+
//!   [1,null,3,2],
9+
//!   [3,2,null,1],
10+
//!   [3,1,null,null,2],
11+
//!   [2,1,3],
12+
//!   [1,null,2,null,3]
13+
//! ]
14+
//! Explanation:
15+
//! The above output corresponds to the 5 unique BST's shown below:
16+
//!
17+
//! 1 3 3 2 1
18+
//! \ / / / \ \
19+
//! 3 2 1 1 3 2
20+
//! / / \ \
21+
//! 2 1 2 3
22+
//!
23+
//! ```
24+
use super::*;
25+
26+
27+
28+
struct Solution{}
29+
impl Solution {
30+
pub fn helper(start: i32, end: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
31+
let mut result = Vec::<Option<Rc<RefCell<TreeNode>>>>::new();
32+
if start > end {
33+
result.push(None);
34+
return result
35+
}
36+
37+
for i in start..=end {
38+
let left_result = Solution::helper(start, i-1);
39+
let right_result = Solution::helper(i+1, end);
40+
41+
for j in 0..left_result.len() {
42+
for k in 0..right_result.len() {
43+
let mut node = TreeNode::new(i);
44+
node.left = left_result[j].clone();
45+
node.right = right_result[k].clone();
46+
result.push(Some(Rc::new(RefCell::new(node))));
47+
}
48+
}
49+
50+
}
51+
52+
result
53+
}
54+
55+
pub fn generate_trees(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
56+
if n == 0 {
57+
vec!()
58+
} else if n == 1 {
59+
vec!(Some(Rc::new(RefCell::new(TreeNode::new(1)))))
60+
} else {
61+
Solution::helper(1, n)
62+
}
63+
}
64+
}
65+
66+
#[cfg(test)]
67+
mod tests {
68+
use super::*;
69+
70+
#[test]
71+
fn test_1() {
72+
Solution::generate_trees(3).iter().map(|x| {
73+
if let Some(v) = x {
74+
println!("{}", (**v).borrow());
75+
println!("-----------------");
76+
}
77+
}).collect::<()>();
78+
assert_eq!(1, 1)
79+
}
80+
}
81+

0 commit comments

Comments
 (0)