LeetCode Symmetric Tree Solution Explained - Java
ฝัง
- เผยแพร่เมื่อ 23 ส.ค. 2024
- The Best Place To Learn Anything Coding Related - bit.ly/3MFZLIZ
Join my free exclusive community built to empower programmers! - www.skool.com/...
Preparing For Your Coding Interviews? Use These Resources
--------------------
(My Course) Data Structures & Algorithms for Coding Interviews - thedailybyte.d...
AlgoCademy - algocademy.com...
Daily Coding Interview Questions - bit.ly/3xw1Sqz
10% Off Of The Best Web Hosting! - hostinger.com/...
Follow Me on X/Twitter - x.com/nickwhit...
Follow My Instagram - / nickwwhite
Other Social Media
----------------------------------------------
Discord - / discord
Twitch - / nickwhitettv
TikTok - / nickwhitetiktok
LinkedIn - / nicholas-w-white
Show Support
------------------------------------------------------------------------------
Patreon - / nick_white
PayPal - paypal.me/nick....
Become A Member - / @nickwhite
#coding #programming #softwareengineering - บันเทิง
Dude you are so underrated ... I can't believe TH-cam is doing it to you! Your explanations are way much better than the rest of TH-camrs
have you even watched kevin naughton jr?
@@nishantsharma3100 can't compare
@@nishantsharma3100 I agree Kevin Naughton explanation was better for this question.
I respect both Kevin and Nick for the amazing teaching content. Some videos are better here and some are better at Kevin's. But I sure do watch both their videos for any solution. They are just amazing. Kudos!!
I am a big fan of your videos! Every time I have a doubt, I check out your videos! In fact, before my interview preparations, every time I am bored and don't want to code, I binge watch your videos.
Are we passing isMirror(root, root) to check if root is null? We could have started with isMirror(root.left, root.right).
i tried it and it get acc
Amazing! I spent about 10 hours to solve this problem, you solved it in 4 minutes. Thanks!
you are literally a gift from heaven. God bless you 😂
Accepted: You have explained better than 99% of the youtubers
Good job. Thanks Nick. I am learning quite a lot from your channel
Iterative solution if anyone interested:
class Solution {
public:
bool isSymmetric(TreeNode *root) {
queue store;
store.push({root, root});
while (!store.empty()) {
pair pr = store.front();
store.pop();
if(pr.first && pr.second){
if(pr.first->val != pr.second->val) return false;
store.push({pr.first->left, pr.second->right});
store.push({pr.first->right, pr.second->left});
} else if(pr.first || pr.second){
return false;
}
}
return true;
}
};
dude excellent!!!!.....i solved it with bfs....but this one was amazing
Leaving a comment bellow because I do like the explanation.
Excellent, Thank you.
In last condition only one isMirror is good enough right?
I had a similar solution, but I added an extra check to see if the left and right val are not equal so that if they are not, it saves on doing the recursive step
class Solution {
public boolean isSymmetric(TreeNode root) {
if(root.left == null && root.right == null) return true;
return isSymmetric(root.left, root.right);
}
private boolean isSymmetric(TreeNode left, TreeNode right){
if(left == null && right == null) return true;
if(left == null || right == null ) return false;
boolean sym = left.val == right.val;
if(!sym) return false;
boolean leftSym = isSymmetric(left.left, right.right);
boolean rightSym = isSymmetric(left.right, right.left);
return sym && leftSym && rightSym;
}
}
and is short circuit operator it won't check for anything else and needlessly recurse if the vals aren't equal
Great explanation!
Nice solution.
Thanks ❤
Thanks boy!
Nick you are not dum
class Solution {
public:
bool checkMirror(TreeNode* root1, TreeNode* root2)
{
if(root1==nullptr && root2==nullptr)
return true;
if(root1==nullptr || root2==nullptr)
return false;
bool isLeftMirror=checkMirror(root1->left,root2->right);
if(!isLeftMirror)
return false;
bool isRightMirror=checkMirror(root1->right,root2->left);
if(!isRightMirror)
return false;
return isLeftMirror && isRightMirror && root1->val==root2->val;
}
bool isSymmetric(TreeNode* root) {
if(root==nullptr)
return true;
if(root->left==nullptr && root->right==nullptr)
return true;
if(root->left==nullptr || root->right==nullptr)
return false;
return checkMirror(root->left,root->right);
}
};
Hey Nick, your video thumbnail is hiding the code
public boolean isSymmetric(TreeNode root) {
return copyMirror(root, root);
}
public boolean copyMirror(TreeNode t1, TreeNode t2) {
if (t1 == null && t2 == null)
return true;
if (t1 == null || t2 == null)
return false;
//we are recursivelly call the left tand right trees
return(t1.val == t2.val) &&
copyMirror(t1.left, t2.right) &&
copyMirror(t1.right, t2.left); //right and left
}
}
### C++ ###
class Solution {
public:
bool isSymmetric(TreeNode* root) {
return root == NULL || isSymmetricHelp(root -> left, root -> right);
}
bool isSymmetricHelp(TreeNode* left, TreeNode* right){
if(left == NULL || right == NULL) return left == right;
if(left -> val != right -> val) return false;
return isSymmetricHelp(left -> left, right -> right) &&
isSymmetricHelp(left -> right, right -> left);
}
};
legend
public class Solution {
public bool IsSymmetric(TreeNode root) {
return isMirror(root, root);
}
public bool isMirror(TreeNode t1, TreeNode t2)
{
if (t1 == null && t2 == null) return true;
if (t1 == null || t2 == null) return false;
return t1.val == t2.val && isMirror(t1.left, t2.right) && isMirror(t1.right, t2.left);
}
}
You can do bfs also right?
A BFS on two copies of the tree just like in this video ? Oone goes left to Right, The other goes right to left ?
hahahhaha, 0:40
U r not dumb bro 😂