Leetcode 894. All Possible Full Binary Trees
ฝัง
- เผยแพร่เมื่อ 15 ก.ย. 2024
- Use coupon ALISHA on any GeeksforGeeks course to get 10% discount:
practice.geeks...
Connect with me on LinkedIn : / alisha-parveen-80579850
Check out our other playlists:
Dynamic Programming:
• Dynamic Programming
Trees:
• Trees
Heaps and Maps:
• Heaps and Maps
Arrays and Maths:
• Arrays and Maths
Bit Manipulation:
• Bit Manipulation
Greedy Algorithms:
• Greedy Algorithms
Sorting and Searching:
• Sorting and Searching
Strings:
• Strings
Linked Lists:
• Linked Lists
Stack and Queues:
• Stacks and Queues
Two Pointers:
• Two pointers
Graphs, BFS, DFS:
• Graphs, DFS, BFS
Backtracking:
• Backtracking
Non- DSA playlists:
Probability:
• Probability
SQL-Basic Join functions:
• SQL - Basic JOIN Funct...
SQL-Basic Aggregate functions:
• SQL-Basic Aggregate Fu...
unordered_mapdp;
vector allPossibleFBT(int n) {
if(n%2==0)return {};
if(n==1)return {new TreeNode()};
if(dp.find(n)!=dp.end())return dp[n];
vectorans;
for(int i=1;ileft = l;
node->right = r;
ans.push_back(node);
}
}
}
return dp[n] = ans;
}
1) explanation up to mark, clear and concise.
2) no asking for like and subscribe. That's why i liked and subscribed to your channel.
3)quality content.
4) after watching your videos it gives me more confidence in dsa.
Thanks 🙏
Thank you !! Almost every problem seems simpler after its explained by you!!
It’s good to see you again 😊
You're back!! And with leetcode premium this time. Bade log. Good to see you back.
I recently discovered this channel
Wow your explanation are so simple and good
Was missing you 😅
Leetcode Queen 👑
nice to see you back your explanations are very good and on point
amazing explanation. thank you.
Thanks Alisha
Happy belated teacher's day ma'am
Ma'am good to see🎉🎉please continue
Good to see you again 🥳🥳
Welcome back after sooo long 😊
Welcome back!!
One video on how to improve communication skills for coding interview
I thought you forgot your Chanel password 😂
😂😂😂
Thank you mam
This channel is alive?
Make video on Print combinations of r elements in an array of size n
God made you beautiful and intelligent ❤. Thank you for the help
finally after many days
Welcome back☺☺
When you completed yr btech and from where ?
message to all learner, dont forget to subscribe to this channel.
Fantastic 🎊👌👌
Why is no one talking about time complexity ?
can anyone pls share time complexity??? I am bit confused
jai shree ram
Are you alive Mam ?
Didi did you got laid off from ms?
Rust implementation ->
// Definition for a binary tree node.
// #[derive(Debug, PartialEq, Eq)]
// pub struct TreeNode {
// pub val: i32,
// pub left: Option,
// pub right: Option,
// }
//
// impl TreeNode {
// #[inline]
// pub fn new(val: i32) -> Self {
// TreeNode {
// val,
// left: None,
// right: None
// }
// }
// }
use std::{rc::Rc, cell::RefCell, collections::HashMap};
type T = Option;
macro_rules! treeNode {
($n:expr) => { Some(Rc::new(RefCell::new(TreeNode::new($n)))) };
($a:expr, $b:expr) => { Some(Rc::new(RefCell::new(TreeNode { val: 0, left: $a, right: $b, })))};
}
impl Solution {
pub fn all_possible_fbt(n: i32) -> Vec {
fn bk(n: i32, dp: &mut HashMap) -> Rc {
match n {
0 => dp.get(&0).unwrap().clone(),
1 => dp.get(&1).unwrap().clone(),
_ if dp.contains_key(&n) => dp.get(&n).unwrap().clone(),
_ => {
let mut res = Vec::new();
for l in (1..n).step_by(2) {
let r = n - 1 - l;
for t1 in bk(l, dp).iter() {
for t2 in bk(r, dp).iter() { res.push(treeNode!(t1.clone(), t2.clone())); }
}
}
let res = Rc::new(res);
dp.insert(n, res.clone());
res
}
}
}
let result = bk( n, &mut HashMap::from([ (0, Rc::new(Vec::::new())), (1, Rc::new(vec![treeNode!(0)])), ]));
Rc::try_unwrap(result).unwrap()
}
}