Symmetric Tree (Leetcode - 101) - (Amazon, Microsoft, Mentor (Siemens)) : Explanation ➕ Live Coding

preview_player
Показать описание
This is the 22nd Video of our Binary Tree Playlist.
In this video we will try to solve a very popular problem "Symmetric Tree".

We will do live coding after explanation and see if we are able to pass all the test cases.

Problem Name : Symmetric Tree
Company Tags : Amazon, Microsoft, Mentor (Siemens)

╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
╠╗║╚╝║║╠╗║╚╣║║║║║═╣
╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
#coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
#interviewpreparation #interview_ds_algo #hinglish
Рекомендации по теме
Комментарии
Автор

Bhai will be popular/famous soon!
So, starting me hi bhai se pehchan karlo through comment! 😊😁
📈👨‍💻 I'll be happy for him! if he becomes popular.

hameedmulani
Автор

Feels great, I am improving day by day.
Thanks

souravjoshi
Автор

I used the String approach you explained in previous video :



class Solution {

String leftSubtreeString(TreeNode root){
if(root == null)
return "N";

return Integer.toString(root.val) + leftSubtreeString(root.left) +
}

String rightSubtreeString(TreeNode root){
if(root == null)
return "N";

return Integer.toString(root.val) + +
}


public boolean isSymmetric(TreeNode root) {

String leftSubtree =
String rightSubtree =

return
}
}

harsh.jain
Автор

thanks for this simple explanation, it became very clear to me

shloksuman
Автор

i swear apki videoss mai ye question ekdam shai kar rha tha bass ek choti si mistake thi ajise hi apki video explanation dekhi ho gaya solve question

tanishqdawar
Автор

Very well explained!! Thank you 😊

Java code

public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
return check(root.left, root.right);
}

public boolean check(TreeNode lnode, TreeNode rnode){
if(lnode == null && rnode == null) return true;
if(lnode == null || rnode == null) return false;

if(lnode.val != rnode.val) return false;
return check(lnode.left, rnode.right) && check(lnode.right, rnode.left);
}

komalkrishna
Автор

Thanks, bhai. Below is the Java Implementation : TC O(N) and space Complexity O(logN) i.e. height of tree (for the recursive stack used)

class Solution {
private boolean solve(TreeNode l, TreeNode r) {

// both of them are null
if(l == null && r == null) {
return true;
}

// only one of them is null
if(l == null || r == null) {
return false;
}

// the values are different
if(l.val != r.val) {
return false;
}

return solve(l.left, r.right) && solve(l.right, r.left);
}
public boolean isSymmetric(TreeNode root) {
return solve(root.left, root.right);
}
}

JJ-tpdd
Автор

Java Code if any one want :

class Solution {

boolean check( TreeNode left, TreeNode right ){

if( left == null && right == null) // if both are null then it is symmetric
return true;

if( left == null || right == null ) // if one of them is true means it is not symmetric
return false;

boolean oneSide = check( left.left, right.right); // checking one side of tree
boolean anotherSide = check( left.right, right.left); //checking anotherSide of tree

if( (left.val == right.val) && oneSide && anotherSide )
return true; // if values are equal and both recursive conditions are equal then also true

return false;
}

public boolean isSymmetric(TreeNode root) {
if (root == null)
return true;

return check( root.left, root.right);
}
}

aadityabuchale
Автор

how to build logic for these type of tree questions? means which approach can we use? how to decide that?

MaheshPatil-ofzy