LeetCode Symmetric Tree Solution Explained - Java

preview_player
Показать описание


Preparing For Your Coding Interviews? Use These Resources
————————————————————

Other Social Media
----------------------------------------------

Show Support
------------------------------------------------------------------------------

#coding #programming #softwareengineering
Рекомендации по теме
Комментарии
Автор

Dude you are so underrated ... I can't believe Youtube is doing it to you! Your explanations are way much better than the rest of Youtubers

djmileno
Автор

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.

aditikaushik
Автор

Are we passing isMirror(root, root) to check if root is null? We could have started with isMirror(root.left, root.right).

oscaropdyou
Автор

Amazing! I spent about 10 hours to solve this problem, you solved it in 4 minutes. Thanks!

JGolt
Автор

you are literally a gift from heaven. God bless you 😂

benakin
Автор

Accepted: You have explained better than 99% of the youtubers

psrs
Автор

Good job. Thanks Nick. I am learning quite a lot from your channel

simonkaranja
Автор

In last condition only one isMirror is good enough right?

_abhishekmj_
Автор

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;
}
}

travisdumont
Автор

Iterative solution if anyone interested:

class Solution {
public:
bool isSymmetric(TreeNode *root) {
queue<pair<TreeNode *, TreeNode *>> store;
store.push({root, root});
while (!store.empty()) {
pair<TreeNode*, TreeNode*> 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;
}
};

NeerajSharma-ozmm
Автор

dude solved it with bfs....but this one was amazing

aayushpagare
Автор

Leaving a comment bellow because I do like the explanation.

GChief
Автор

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);
}
};

supratimbhattacharjee
Автор

Hey Nick, your video thumbnail is hiding the code

Anand
Автор

### 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);

}
};

crispyclips
Автор

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);
}
}

mohammedghabyen