Skip to content

feat: add solutions to lc problems: No.0894,0906 #2527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 67 additions & 39 deletions solution/0800-0899/0894.All Possible Full Binary Trees/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

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

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

<!-- tabs:start -->

Expand Down Expand Up @@ -254,65 +254,93 @@ function allPossibleFBT(n: number): Array<TreeNode | null> {
// }
// }
// }

impl TreeNode {
pub fn new_with_node(
left: Option<Rc<RefCell<TreeNode>>>,
right: Option<Rc<RefCell<TreeNode>>>
) -> Self {
Self {
val: 0,
left,
right,
}
}
}

use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
#[allow(dead_code)]
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
let mut record_vec = vec![vec![]; n as usize + 1];
Self::dfs(n, &mut record_vec)
let mut f: Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>> = vec![None; (n + 1) as usize];
Self::dfs(n, &mut f)
}

#[allow(dead_code)]
fn dfs(
n: i32,
record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>
f: &mut Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>>
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
if record_vec[n as usize].len() != 0 {
return record_vec[n as usize].clone();
if let Some(ref result) = f[n as usize] {
return result.clone();
}

let mut ans = Vec::new();
if n == 1 {
// Just directly return a single node
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
ans.push(Some(Rc::new(RefCell::new(TreeNode::new(0)))));
return ans;
}
// Otherwise, need to construct return vector
let mut ret_vec = Vec::new();

// Enumerate the node number for left subtree from 0 -> n - 1
for i in 0..n - 1 {
// The number of right subtree node
let j = n - i - 1;
for left in Self::dfs(i, record_vec) {
for right in Self::dfs(j, record_vec) {
// Construct the ret vector
ret_vec.push(
Some(
Rc::new(
RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))
)
let j = n - 1 - i;
for left in Self::dfs(i, f).iter() {
for right in Self::dfs(j, f).iter() {
let new_node = Some(
Rc::new(
RefCell::new(TreeNode {
val: 0,
left: left.clone(),
right: right.clone(),
})
)
);
ans.push(new_node);
}
}
}
f[n as usize] = Some(ans.clone());
ans
}
}
```

record_vec[n as usize] = ret_vec;
```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private List<TreeNode>[] f;

public IList<TreeNode> AllPossibleFBT(int n) {
f = new List<TreeNode>[n + 1];
return Dfs(n);
}

private IList<TreeNode> Dfs(int n) {
if (f[n] != null) {
return f[n];
}

record_vec[n as usize].clone()
if (n == 1) {
return new List<TreeNode> { new TreeNode() };
}

List<TreeNode> ans = new List<TreeNode>();
for (int i = 0; i < n - 1; ++i) {
int j = n - 1 - i;
foreach (var left in Dfs(i)) {
foreach (var right in Dfs(j)) {
ans.Add(new TreeNode(0, left, right));
}
}
}
f[n] = ans;
return ans;
}
}
```
Expand Down
114 changes: 75 additions & 39 deletions solution/0800-0899/0894.All Possible Full Binary Trees/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@

## Solutions

### Solution 1
### Solution 1: Memoization Search

If $n=1$, return a list with a single node directly.

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.

This process can be optimized with memoization search to avoid repeated calculations.

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.

<!-- tabs:start -->

Expand Down Expand Up @@ -242,65 +250,93 @@ function allPossibleFBT(n: number): Array<TreeNode | null> {
// }
// }
// }

impl TreeNode {
pub fn new_with_node(
left: Option<Rc<RefCell<TreeNode>>>,
right: Option<Rc<RefCell<TreeNode>>>
) -> Self {
Self {
val: 0,
left,
right,
}
}
}

use std::rc::Rc;
use std::cell::RefCell;
impl Solution {
#[allow(dead_code)]
pub fn all_possible_fbt(n: i32) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
let mut record_vec = vec![vec![]; n as usize + 1];
Self::dfs(n, &mut record_vec)
let mut f: Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>> = vec![None; (n + 1) as usize];
Self::dfs(n, &mut f)
}

#[allow(dead_code)]
fn dfs(
n: i32,
record_vec: &mut Vec<Vec<Option<Rc<RefCell<TreeNode>>>>>
f: &mut Vec<Option<Vec<Option<Rc<RefCell<TreeNode>>>>>>
) -> Vec<Option<Rc<RefCell<TreeNode>>>> {
if record_vec[n as usize].len() != 0 {
return record_vec[n as usize].clone();
if let Some(ref result) = f[n as usize] {
return result.clone();
}

let mut ans = Vec::new();
if n == 1 {
// Just directly return a single node
return vec![Some(Rc::new(RefCell::new(TreeNode::new(0))))];
ans.push(Some(Rc::new(RefCell::new(TreeNode::new(0)))));
return ans;
}
// Otherwise, need to construct return vector
let mut ret_vec = Vec::new();

// Enumerate the node number for left subtree from 0 -> n - 1
for i in 0..n - 1 {
// The number of right subtree node
let j = n - i - 1;
for left in Self::dfs(i, record_vec) {
for right in Self::dfs(j, record_vec) {
// Construct the ret vector
ret_vec.push(
Some(
Rc::new(
RefCell::new(TreeNode::new_with_node(left.clone(), right.clone()))
)
let j = n - 1 - i;
for left in Self::dfs(i, f).iter() {
for right in Self::dfs(j, f).iter() {
let new_node = Some(
Rc::new(
RefCell::new(TreeNode {
val: 0,
left: left.clone(),
right: right.clone(),
})
)
);
ans.push(new_node);
}
}
}
f[n as usize] = Some(ans.clone());
ans
}
}
```

record_vec[n as usize] = ret_vec;
```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private List<TreeNode>[] f;

public IList<TreeNode> AllPossibleFBT(int n) {
f = new List<TreeNode>[n + 1];
return Dfs(n);
}

private IList<TreeNode> Dfs(int n) {
if (f[n] != null) {
return f[n];
}

record_vec[n as usize].clone()
if (n == 1) {
return new List<TreeNode> { new TreeNode() };
}

List<TreeNode> ans = new List<TreeNode>();
for (int i = 0; i < n - 1; ++i) {
int j = n - 1 - i;
foreach (var left in Dfs(i)) {
foreach (var right in Dfs(j)) {
ans.Add(new TreeNode(0, left, right));
}
}
}
f[n] = ans;
return ans;
}
}
```
Expand Down
43 changes: 43 additions & 0 deletions solution/0800-0899/0894.All Possible Full Binary Trees/Solution.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private List<TreeNode>[] f;

public IList<TreeNode> AllPossibleFBT(int n) {
f = new List<TreeNode>[n + 1];
return Dfs(n);
}

private IList<TreeNode> Dfs(int n) {
if (f[n] != null) {
return f[n];
}

if (n == 1) {
return new List<TreeNode> { new TreeNode() };
}

List<TreeNode> ans = new List<TreeNode>();
for (int i = 0; i < n - 1; ++i) {
int j = n - 1 - i;
foreach (var left in Dfs(i)) {
foreach (var right in Dfs(j)) {
ans.Add(new TreeNode(0, left, right));
}
}
}
f[n] = ans;
return ans;
}
}
Loading