Skip to content

Commit 3fec0c4

Browse files
authoredApr 2, 2024··
feat: add solutions to lc problems: No.0894,0906 (#2527)
* No.0894.All Possible Full Binary Trees * No.0906.Super Palindromes
1 parent 98cf993 commit 3fec0c4

File tree

13 files changed

+761
-118
lines changed

13 files changed

+761
-118
lines changed
 

‎solution/0800-0899/0894.All Possible Full Binary Trees/README.md

+67-39
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848

4949
此过程可以用记忆化搜索,避免重复计算。
5050

51-
时间复杂度 $O(2^n)$,空间复杂度 $O(2^n)$。其中 $n$ 是节点数量。
51+
时间复杂度 $O(\frac{2^n}{\sqrt{n}})$,空间复杂度 $O(\frac{2^n}{\sqrt{n}})$。其中 $n$ 是节点数量。
5252

5353
<!-- tabs:start -->
5454

@@ -254,65 +254,93 @@ function allPossibleFBT(n: number): Array<TreeNode | null> {
254254
// }
255255
// }
256256
// }
257-
258-
impl TreeNode {
259-
pub fn new_with_node(
260-
left: Option<Rc<RefCell<TreeNode>>>,
261-
right: Option<Rc<RefCell<TreeNode>>>
262-
) -> Self {
263-
Self {
264-
val: 0,
265-
left,
266-
right,
267-
}
268-
}
269-
}
270-
271257
use std::rc::Rc;
272258
use std::cell::RefCell;
273259
impl Solution {
274-
#[allow(dead_code)]
275260
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
276-
let mut record_vec = vec![vec![]; n as usize + 1];
277-
Self::dfs(n, &mut record_vec)
261+
let mut f: Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>> = vec![None; (n + 1) as usize];
262+
Self::dfs(n, &mut f)
278263
}
279264

280-
#[allow(dead_code)]
281265
fn dfs(
282266
n: i32,
283-
record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>
267+
f: &mut Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>>
284268
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
285-
if record_vec[n as usize].len() != 0 {
286-
return record_vec[n as usize].clone();
269+
if let Some(ref result) = f[n as usize] {
270+
return result.clone();
287271
}
272+
273+
let mut ans = Vec::new();
288274
if n == 1 {
289-
// Just directly return a single node
290-
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
275+
ans.push(Some(Rc::new(RefCell::new(TreeNode::new(0)))));
276+
return ans;
291277
}
292-
// Otherwise, need to construct return vector
293-
let mut ret_vec = Vec::new();
294278

295-
// Enumerate the node number for left subtree from 0 -> n - 1
296279
for i in 0..n - 1 {
297-
// The number of right subtree node
298-
let j = n - i - 1;
299-
for left in Self::dfs(i, record_vec) {
300-
for right in Self::dfs(j, record_vec) {
301-
// Construct the ret vector
302-
ret_vec.push(
303-
Some(
304-
Rc::new(
305-
RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))
306-
)
280+
let j = n - 1 - i;
281+
for left in Self::dfs(i, f).iter() {
282+
for right in Self::dfs(j, f).iter() {
283+
let new_node = Some(
284+
Rc::new(
285+
RefCell::new(TreeNode {
286+
val: 0,
287+
left: left.clone(),
288+
right: right.clone(),
289+
})
307290
)
308291
);
292+
ans.push(new_node);
309293
}
310294
}
311295
}
296+
f[n as usize] = Some(ans.clone());
297+
ans
298+
}
299+
}
300+
```
312301

313-
record_vec[n as usize] = ret_vec;
302+
```cs
303+
/**
304+
* Definition for a binary tree node.
305+
* public class TreeNode {
306+
* public int val;
307+
* public TreeNode left;
308+
* public TreeNode right;
309+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
310+
* this.val = val;
311+
* this.left = left;
312+
* this.right = right;
313+
* }
314+
* }
315+
*/
316+
public class Solution {
317+
private List<TreeNode>[] f;
318+
319+
public IList<TreeNode> AllPossibleFBT(int n) {
320+
f = new List<TreeNode>[n + 1];
321+
return Dfs(n);
322+
}
323+
324+
private IList<TreeNode> Dfs(int n) {
325+
if (f[n] != null) {
326+
return f[n];
327+
}
314328

315-
record_vec[n as usize].clone()
329+
if (n == 1) {
330+
return new List<TreeNode> { new TreeNode() };
331+
}
332+
333+
List<TreeNode> ans = new List<TreeNode>();
334+
for (int i = 0; i < n - 1; ++i) {
335+
int j = n - 1 - i;
336+
foreach (var left in Dfs(i)) {
337+
foreach (var right in Dfs(j)) {
338+
ans.Add(new TreeNode(0, left, right));
339+
}
340+
}
341+
}
342+
f[n] = ans;
343+
return ans;
316344
}
317345
}
318346
```

‎solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md

+75-39
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@
3636

3737
## Solutions
3838

39-
### Solution 1
39+
### Solution 1: Memoization Search
40+
41+
If $n=1$, return a list with a single node directly.
42+
43+
If $n > 1$, we can enumerate the number of nodes $i$ in the left subtree, then the number of nodes in the right subtree is $n-1-i$. For each case, we recursively construct all possible genuine binary trees for the left and right subtrees. Then we combine the left and right subtrees in pairs to get all possible genuine binary trees.
44+
45+
This process can be optimized with memoization search to avoid repeated calculations.
46+
47+
The time complexity is $O(\frac{2^n}{\sqrt{n}})$, and the space complexity is $O(\frac{2^n}{\sqrt{n}})$. Where $n$ is the number of nodes.
4048

4149
<!-- tabs:start -->
4250

@@ -242,65 +250,93 @@ function allPossibleFBT(n: number): Array<TreeNode | null> {
242250
// }
243251
// }
244252
// }
245-
246-
impl TreeNode {
247-
pub fn new_with_node(
248-
left: Option<Rc<RefCell<TreeNode>>>,
249-
right: Option<Rc<RefCell<TreeNode>>>
250-
) -> Self {
251-
Self {
252-
val: 0,
253-
left,
254-
right,
255-
}
256-
}
257-
}
258-
259253
use std::rc::Rc;
260254
use std::cell::RefCell;
261255
impl Solution {
262-
#[allow(dead_code)]
263256
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
264-
let mut record_vec = vec![vec![]; n as usize + 1];
265-
Self::dfs(n, &mut record_vec)
257+
let mut f: Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>> = vec![None; (n + 1) as usize];
258+
Self::dfs(n, &mut f)
266259
}
267260

268-
#[allow(dead_code)]
269261
fn dfs(
270262
n: i32,
271-
record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>
263+
f: &mut Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>>
272264
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
273-
if record_vec[n as usize].len() != 0 {
274-
return record_vec[n as usize].clone();
265+
if let Some(ref result) = f[n as usize] {
266+
return result.clone();
275267
}
268+
269+
let mut ans = Vec::new();
276270
if n == 1 {
277-
// Just directly return a single node
278-
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
271+
ans.push(Some(Rc::new(RefCell::new(TreeNode::new(0)))));
272+
return ans;
279273
}
280-
// Otherwise, need to construct return vector
281-
let mut ret_vec = Vec::new();
282274

283-
// Enumerate the node number for left subtree from 0 -> n - 1
284275
for i in 0..n - 1 {
285-
// The number of right subtree node
286-
let j = n - i - 1;
287-
for left in Self::dfs(i, record_vec) {
288-
for right in Self::dfs(j, record_vec) {
289-
// Construct the ret vector
290-
ret_vec.push(
291-
Some(
292-
Rc::new(
293-
RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))
294-
)
276+
let j = n - 1 - i;
277+
for left in Self::dfs(i, f).iter() {
278+
for right in Self::dfs(j, f).iter() {
279+
let new_node = Some(
280+
Rc::new(
281+
RefCell::new(TreeNode {
282+
val: 0,
283+
left: left.clone(),
284+
right: right.clone(),
285+
})
295286
)
296287
);
288+
ans.push(new_node);
297289
}
298290
}
299291
}
292+
f[n as usize] = Some(ans.clone());
293+
ans
294+
}
295+
}
296+
```
300297

301-
record_vec[n as usize] = ret_vec;
298+
```cs
299+
/**
300+
* Definition for a binary tree node.
301+
* public class TreeNode {
302+
* public int val;
303+
* public TreeNode left;
304+
* public TreeNode right;
305+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
306+
* this.val = val;
307+
* this.left = left;
308+
* this.right = right;
309+
* }
310+
* }
311+
*/
312+
public class Solution {
313+
private List<TreeNode>[] f;
314+
315+
public IList<TreeNode> AllPossibleFBT(int n) {
316+
f = new List<TreeNode>[n + 1];
317+
return Dfs(n);
318+
}
319+
320+
private IList<TreeNode> Dfs(int n) {
321+
if (f[n] != null) {
322+
return f[n];
323+
}
302324

303-
record_vec[n as usize].clone()
325+
if (n == 1) {
326+
return new List<TreeNode> { new TreeNode() };
327+
}
328+
329+
List<TreeNode> ans = new List<TreeNode>();
330+
for (int i = 0; i < n - 1; ++i) {
331+
int j = n - 1 - i;
332+
foreach (var left in Dfs(i)) {
333+
foreach (var right in Dfs(j)) {
334+
ans.Add(new TreeNode(0, left, right));
335+
}
336+
}
337+
}
338+
f[n] = ans;
339+
return ans;
304340
}
305341
}
306342
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* public int val;
5+
* public TreeNode left;
6+
* public TreeNode right;
7+
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
8+
* this.val = val;
9+
* this.left = left;
10+
* this.right = right;
11+
* }
12+
* }
13+
*/
14+
public class Solution {
15+
private List<TreeNode>[] f;
16+
17+
public IList<TreeNode> AllPossibleFBT(int n) {
18+
f = new List<TreeNode>[n + 1];
19+
return Dfs(n);
20+
}
21+
22+
private IList<TreeNode> Dfs(int n) {
23+
if (f[n] != null) {
24+
return f[n];
25+
}
26+
27+
if (n == 1) {
28+
return new List<TreeNode> { new TreeNode() };
29+
}
30+
31+
List<TreeNode> ans = new List<TreeNode>();
32+
for (int i = 0; i < n - 1; ++i) {
33+
int j = n - 1 - i;
34+
foreach (var left in Dfs(i)) {
35+
foreach (var right in Dfs(j)) {
36+
ans.Add(new TreeNode(0, left, right));
37+
}
38+
}
39+
}
40+
f[n] = ans;
41+
return ans;
42+
}
43+
}

0 commit comments

Comments
 (0)
Please sign in to comment.