Тёмный

L25. Check for Symmetrical Binary Trees | C++ | Java 

take U forward
Подписаться 682 тыс.
Просмотров 155 тыс.
50% 1

Опубликовано:

 

30 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 198   
@takeUforward
@takeUforward 3 года назад
Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79
@peerless3538
@peerless3538 2 года назад
Please reduce the quality of videos 🙃 your content is pretty awesome💖😋 but in the end when you show the code, it consumes lot of data(1080P) to see the code clearly 🥺
@Ayush37262
@Ayush37262 Месяц назад
​@@peerless3538 Are u mad?
@peerless3538
@peerless3538 Месяц назад
@@Ayush37262 no actually u r
@shubham_paliwal
@shubham_paliwal 10 месяцев назад
Great Explanation 👏 I had a doubt though, at 3:34, you said Inorder traversal as Root -> Left -> Right, which to my knowledge is Preorder traversal I suppose, and Inorder traversal is actually Left-> Root -> Right. I can understand that it must have occurred by mistake, it's quite obvious that the unrivaled king of DSA can't commit this kind of mistake, it must have gone unnoticed. I request you to correct it, otherwise, it may cause confusion to beginners.
@mohit7717
@mohit7717 3 месяца назад
That's mistake but after that he cover we can use similar traversal on right subtree ... we can use any traversal but same on both subtree whether its inorder, preorder or postorder,
@lifehustlers164
@lifehustlers164 Год назад
Completed 26/54 (48% done) !!!
@uRamPlus
@uRamPlus 2 года назад
Self Notes: 💡 Mirror property is left == right and right == left 💡 pre-order traversal on root->left subtree, (root, left, right) 💡 modified pre-order traversal on root->right subtree, (root, right, left) 💡 compare the node val's if they are the same 💡 Do both traversals at the same time 💡 if left is null or right is null, then both sides must match and return true (base case)
@hyperme1831
@hyperme1831 4 месяца назад
I don't understand one thing in worst case why space complexity is O(n) even in normal case also we will traverse all the nodes so ot will be O(n) there is no difference right
@dhruvchopra26
@dhruvchopra26 4 месяца назад
@@hyperme1831 The recursive stack space is O(Height) because at max no. of recursion calls in the stack will be = height. so in case of full binary tree(the one you are referring to as normal case) ,say with 3 levels(and 2^i nodes in each level where i is the level no. starting from 0); the SC=O(3). But in worst case(i.e. skew tree) it height=n. Therefore SC=O(N) in that case.
@thivyaamohan3671
@thivyaamohan3671 2 года назад
Did anyone notice that -> should be used instead of "." in c++ code??
@amitgupta2890
@amitgupta2890 2 года назад
and null / NULL;
@abhinavtripathi9678
@abhinavtripathi9678 Год назад
Depends whether you have object/struct or a pointer to it. If you have the object or structure you may use "." If you have pointer you may use ->
@muditkhanna8164
@muditkhanna8164 Год назад
its java
@madgepereira2891
@madgepereira2891 9 месяцев назад
Yep , you are right
@indubalaarora846
@indubalaarora846 3 месяца назад
Does that effect ​@@amitgupta2890
@lushabhsaxena6674
@lushabhsaxena6674 2 года назад
I think in the skewed example, your code will return in the very first step of the recursion. So, here the space complexity cannot be O(n). I believe the worst case would be O(log n) for a full binary tree that is say symmetric till the we reach one of the leaf node. Please clarify this aspect.
@pratikshadhole6694
@pratikshadhole6694 Год назад
yes, I do think the same
@factfactorial632
@factfactorial632 2 года назад
time complexity should be more accurately O(N/2) and space complexity O(N/2) because we are traveling left and right simultaneously
@tusharagarwal6155
@tusharagarwal6155 Год назад
Doesn't matter dude...In Big-O time complexity constants are neglected anyways.
@jayeshpaliwal2258
@jayeshpaliwal2258 3 года назад
Understood but C++ code have Java syntax.(left.left)
@iamnottech8918
@iamnottech8918 3 месяца назад
Here is my solution using same approach bool isSame(TreeNode* p, TreeNode* q) { if (p == NULL && q == NULL) return true; if (p == NULL || q == NULL) return false; bool left = isSame(p->left, q->right); bool right = isSame(p->right, q->left); if (p->val != q->val) return false; return left && right; } bool isSymmetric(TreeNode* root) { if (root == NULL) return true; return isSame(root->left, root->right); }
@b_1729-j8j
@b_1729-j8j 2 года назад
I understood TC but for SC in case of skewed BT then algorithm will stop at top itself since there are not nulls on both sides then how it will be O(n)? Correct me if I am missing anything.
@ekanshsanger8356
@ekanshsanger8356 2 года назад
Of what I think..... In case of left skewed tree, lh=some node but rh=NULL; In this piece of code, as rh is NULL it will have no val data member as such. if(lh->val != rh->val) return false; (A node pointing to NULL does not have any data members i.e.-: left,right or val). This is the reason we, every single time gives a base case - if(lh==NULL), to specify an exceptional case in terms, that if the node becomes NULL. So the execution skips this part - if(lh->val != rh->val) return false; and helper(lh->left,rh->right) begins to execute which stores a recursion stack space of left tree as O(N). So, SC - O(N).
@ekanshsanger8356
@ekanshsanger8356 2 года назад
class Solution { public: bool helper(TreeNode* lh,TreeNode* rh){ if(lh==NULL){ if(rh==NULL) return true; else return false; } if(rh==NULL){ if(lh==NULL) return true; else return false; } if(lh->val != rh->val) return false; if(!helper(lh->left,rh->right) || !helper(lh->right,rh->left)) return false; return true; } bool isSymmetric(TreeNode* root) { TreeNode* lh=root->left; TreeNode* rh=root->right; return helper(lh,rh); } };
@lushabhsaxena6674
@lushabhsaxena6674 2 года назад
I also thought the same. The worst case would be when we have a complete binary tree and the symmetric property is satisfied completely. In that case the worst case should be O(log n). Could some one correct me or clarify this aspect.
@sarvottampriyadarshee5425
@sarvottampriyadarshee5425 2 года назад
it'll be O(N) SC because the recursion is a DFS based -> think about the case when the tree has N / 2 nodes to the left of root (like a linked list, going towards left -> left -> left) and N / 2 nodes to the right of root (like a linked list, going towards right -> right -> right) ... now our recursion condition is ((return dfs(leftSubtree -> left, rightSubtree -> right) && ...)) so in this case -> the recursion will go deep to the last node -> basically O(N / 2) space will be used which will be O(N) to calculate, then only after returning -> the 2nd condition after (&& ...) will be checked for every recursion call -> and the answer eventually becomes false ... but the point is -> it did takes O(N) space I didn't mention it earlier but -> Of course put the 1 wrong placed node at the bottom of either the left subtree or the right subtree -> then it will return false from the bottom
@altamashsabri8142
@altamashsabri8142 2 года назад
Imagine a Tree having only left childs in left side of root and right childs in right side of root then TC & SC both will be O(N)
@aakriti1
@aakriti1 2 года назад
Somewhat similar to the question check whether 2 trees are identical or not with some modifications :)
@nagame859
@nagame859 Год назад
Absolutely!
@605_samikrdas4
@605_samikrdas4 2 года назад
incase you need the code: bool isSymmetric(struct Node* root) { // Code here return (root==NULL || isSymmetricHelp(root->left,root->right)); } bool isSymmetricHelp(Node* left, Node* right) { if(left==NULL || right==NULL) return left==right; if(left->data!= right->data) return false; return isSymmetricHelp(left->left, right->right) && isSymmetricHelp(left->right, right->left); }
@ManyaNayak-md7xr
@ManyaNayak-md7xr 4 месяца назад
can you explain why in function isSymmetricHelp in first condition we have used || and return left==right...can we use && instead and return true; i have tried using && and the code gives runntime error..i want to know why is it wrong?
@monikaraut5266
@monikaraut5266 Год назад
space complexity will not be O(n) for skewed tree because when it’s skewed tree it will return false in the very first comparison.
@rohangangwar6604
@rohangangwar6604 3 года назад
bhaiya pdhate padhate bhul jaate hain ki inorder bolna hai ya preorder at 3:28 bhaiya likh preorder rhe hai or bol inorder rhe hain...no matter hume smjh aagya kaafi hai.. thank you bhaiya maza aagya
@takeUforward
@takeUforward 3 года назад
Haan wo ek stretch me record krte h na and raat 3/4 baje th thoda ho jaata
@rohangangwar6604
@rohangangwar6604 3 года назад
@@takeUforward its ohk bhaiya maza aagya videos dekh ke bht acha h
@mohdhasnain3812
@mohdhasnain3812 3 года назад
@@takeUforward bhai you are living legend recently i got package for 7lpa but i will really grind up in my last 6 months and then update you with good news.
@zaidachmed868
@zaidachmed868 2 года назад
@@mohdhasnain3812 updates hasnain?
@dikshantsharma3038
@dikshantsharma3038 11 месяцев назад
@@zaidachmed868 no updates
@babulalyadav4305
@babulalyadav4305 3 месяца назад
00:01 Check if a binary tree is symmetric 00:50 Discussing about symmetrical binary trees in C++ and Java 02:00 Checking for symmetrical binary trees in C++ and Java 03:20 Understanding symmetrical binary trees 04:37 Check for Symmetrical Binary Trees 05:40 Symmetry is important in creating effective and efficient programs. 06:47 Check for Symmetry in Binary Trees 08:18 The video discusses checking for symmetrical binary trees in C++ and Java.
@AMITKUMAR-dj2fv
@AMITKUMAR-dj2fv Год назад
Iterative traversal code to check for symmetry in binary tree: bool isSymmetric(TreeNode* root) { if(root==nullptr)return true; queueq; q.push(root); while(!q.empty()){ int x=q.size(); vectortemp; for(int i=0;ival); if(curr->left){ q.push(curr->left); }else q.push(nullptr); if(curr->right){ q.push(curr->right); }else q.push(nullptr); } else temp.push_back(101); } int n=temp.size(); if(n>1){ int l=0,r=n-1; while(l
@ManyaNayak-md7xr
@ManyaNayak-md7xr 4 месяца назад
can you explain why in function isSymmetricHelp in first condition we have used || and return left==right...can we use && instead and return true; i have tried using && and the code gives runntime error..i want to know why is it wrong?
@jotsinghbindra8317
@jotsinghbindra8317 3 месяца назад
@@ManyaNayak-md7xr if you use && you cover only 1 case that is both are null but there are two more cases one is null second not..second is null first not ...so by taking || we cover all the three cases at once
@rishabhverma7730
@rishabhverma7730 3 года назад
So smooth so.... Lovely... Now I can overcome the Scariest fear in coding
@yashlakade179
@yashlakade179 2 года назад
Ohh.....A astrange thing happen with me first I tried these problem and successfully solved it with O(N) time and O(1) space. and then what the same code was written by Striver Bhaiyya too......
@neerajgarg9096
@neerajgarg9096 2 года назад
*for those who are facing difficulty like me in understanding the code* class Solution{ private: bool isSymmetricfast(Node* rootleft,Node* rootright){ if(rootleft==NULL && rootright==NULL){ return true; } if(rootleft!=NULL && rootright==NULL){ return false; } if(rootleft==NULL && rootright!=NULL){ return false; } if(rootleft->data!=rootright->data){ return false; } bool cnd1 = isSymmetricfast(rootleft->left,rootright->right); bool cnd2 = isSymmetricfast(rootleft->right,rootright->left); if(cnd1==true && cnd2==true){ return true; } else{ return false; } } public: // return true/false denoting whether the tree is Symmetric or not bool isSymmetric(struct Node* root) { if(root==NULL){ return true; } return isSymmetricfast(root->left,root->right); } };
@Wanderingfitgeek
@Wanderingfitgeek 2 года назад
your code really helped me in understanding the concept. thank you!!
@sahulraj9536
@sahulraj9536 6 месяцев назад
we can still optimize a little bit, instead of sending root both the times as arguments, we should send root-->left, root->right. because the first approach checks both sides of the root.(left and right).we dont need that.if we check for one side its enough. bool isSym(TreeNode*root1, TreeNode*root2) { if(root1 == NULL && root2 == NULL)return true; if(root1 == NULL || root2 == NULL)return false; if(root1->val != root2->val)return false; if(isSym(root1->left, root2->right) == false) return false; if(isSym(root1->right, root2->left) == false) return false; return true; } bool isSymmetric(TreeNode* root) { TreeNode*root1 = root->left; TreeNode*root2 = root->right; if(root1 == NULL && root2 == NULL)return true; if(root1 == NULL || root2 == NULL)return false; return isSym(root1, root2); }
@crispyclips2916
@crispyclips2916 Год назад
### 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); } };
@shobhitsingh8695
@shobhitsingh8695 11 месяцев назад
Preorder is equal to reverse of postorder when the tree is symmetric just add some value for null when you return when reach null
@deepakojha8431
@deepakojha8431 3 года назад
Bhaiya please playlist m sare questions cover krna jo product based companies m puchhe jate hain....
@takeUforward
@takeUforward 3 года назад
Sare hi kar rahe hai..
@deepakojha8431
@deepakojha8431 3 года назад
@@takeUforward thank you bhaiya..... Love u ❣️❣️
@sarathchandra941
@sarathchandra941 2 года назад
So its a preorder traversal.
@dipeshsaili4468
@dipeshsaili4468 2 года назад
Self Notes: 💡 Mirror property is left == right and right == left 💡 pre-order traversal on root->left subtree, (root, left, right) 💡 modified pre-order traversal on root->right subtree, (root, right, left) 💡 compare the node val's if they are the same 💡 Do both traversals at the same time 💡 if left is null or right is null, then both sides must match and return true (base case)
@janakiramank6281
@janakiramank6281 2 года назад
could you pls help me out to find below tree symmetric or not.? root = new Node(1); root->left = new Node(2); root->left->left = new Node(4); root->left->right = new Node(5); root->right = new Node(2); root->right->left = new Node(5); root->right->right = new Node(41);
@shashankverma5108
@shashankverma5108 Год назад
Iterative Solution C++ bool isSymmetric(TreeNode* root) { if(!root) return true; queue q1; queue q2; if(root->left && root->right) { q1.push(root->left); q2.push(root->right); } else if(root->left || root->right) return false; while(!q1.empty() && !q2.empty()) { int s1 = q1.size(); int s2 = q2.size(); if(s1 != s2) return false; for(int i=0;ival != p2->val) return false; if(p1->left && p2->right) { q1.push(p1->left); q2.push(p2->right); } else if(p1->left || p2->right) return false; if(p1->right && p2->left) { q1.push(p1->right); q2.push(p2->left); } else if(p1->right || p2->left) return false; } } return true; }
@adityan5302
@adityan5302 2 года назад
PYTHON CODE : I think this is most optimised than the above one. No offense. It's the same concept he taught us in one of the video def solve(roota, rootb): if (roota==None) or (rootb==None): return roota==rootb if roota.data!=rootb.data: return False l = solve(roota.left, rootb.right) if l==False: return False r = solve(roota.right, rootb.left) if r==False: return False if l==False and r==False: return l return True def isSymmetrical(root): return (root==None) or solve(root.left, root.right)
@devbhattacharya153
@devbhattacharya153 2 года назад
Thanks bro
@adityan5302
@adityan5302 2 года назад
@@devbhattacharya153 it's ok bro
@stith_pragya
@stith_pragya 11 месяцев назад
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@manita544
@manita544 Год назад
Simple answer Code: class Solution { public: bool isSymmetric(TreeNode* rootLeft, TreeNode* rootRight) { if (rootLeft == NULL && rootRight == NULL) return true; if (rootLeft == NULL || rootRight == NULL) return false; if (rootLeft->val != rootRight->val) return false; return isSymmetric(rootLeft->left, rootRight->right) && isSymmetric(rootLeft->right, rootRight->left); } bool isSymmetric(TreeNode* root) { if (root == NULL) return true; return isSymmetric(root->left, root->right); } };
@prabhakaran5542
@prabhakaran5542 Месяц назад
Understood ❤
@debajyotisaha1666
@debajyotisaha1666 Год назад
done this using inorder traversal of tree void inorder(TreeNode* node,vector &tr,int level) { if(node==NULL) { return; } inorder(node->left,tr,level+1); tr.push_back({node->val,level}); inorder(node->right,tr,level+1); } bool isSymmetric(TreeNode* root) { vector ans; inorder(root,ans,0); //cout
@mutthikarunakar8951
@mutthikarunakar8951 2 года назад
this code will not pass all test cases it will not pass the test case if (right !=null &&left==null) || (right==null&&left !=null) return false; please add this base case
@sharmanihal99
@sharmanihal99 4 месяца назад
3 Approaches to solve this (1 DFS and 2 BFS) # Definition for a binary tree node. # class TreeNode: # def __init__(self, val=0, left=None, right=None): # self.val = val # self.left = left # self.right = right class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: if not root: # If the tree is empty, it's symmetric return True ac # Use a helper function to perform DFS and check symmetry return self.dfs(root.left, root.right) def dfs(self, root1, root2): if not root1 or not root2: # If either node is null, check if both are null return root1 == root2 if root1.val != root2.val: # If the values of nodes are different, it's not symmetric return False # Recursively check the symmetry of the left and right subtrees return self.dfs(root1.left, root2.right) and self.dfs(root1.right, root2.left) class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: if not root: # If the tree is empty, it's symmetric return True if not root.left or not root.right: # If either subtree is null, check if both are null return root.left == root.right # Use two queues to perform BFS and check symmetry queue1 = deque([root.left]) queue2 = deque([root.right]) while queue1 and queue2: # While both queues are not empty node1 = queue1.popleft() # Get the front node of the first queue node2 = queue2.popleft() # Get the front node of the second queue if node1.val != node2.val: # If the values of nodes are different, it's not symmetric return False # Check the right child of the first node with the left child of the second node if node1.right and node2.left: queue1.append(node1.right) queue2.append(node2.left) elif node1.right or node2.left: # If only one of them is null, it's not symmetric return False # Check the left child of the first node with the right child of the second node if node1.left and node2.right: queue1.append(node1.left) queue2.append(node2.right) elif node1.left or node2.right: # If only one of them is null, it's not symmetric return False return True class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: if not root: # If the tree is empty, it's symmetric return True if not root.left or not root.right: # If either subtree is null, check if both are null return root.left == root.right # Use a queue to perform BFS and check symmetry queue = deque([(root.left, root.right)]) while queue: # While the queue is not empty root1, root2 = queue.popleft() # Get the front pair of nodes if not root1 and not root2: # If both nodes are null, continue to the next pair continue if not root1 or not root2: # If only one of them is null, it's not symmetric return False if root1.val != root2.val: # If the values of nodes are different, it's not symmetric return False # Append the children of the current pair of nodes to the queue for further checking queue.append((root1.left, root2.right)) queue.append((root1.right, root2.left)) return True
@UECAshutoshKumar
@UECAshutoshKumar Год назад
Thank you sir
@anishaa3298
@anishaa3298 6 месяцев назад
thank you so much! understood immediately!
@ankiteshji
@ankiteshji Год назад
Bhaiya kyun confuse kr rhe ho😂😂😂😂... Inorder apne hi bataya tha ki left-root-right hota hai..
@tarunpratap3980
@tarunpratap3980 3 месяца назад
why cant we do it like doing inordr traversal and then check for pallindrome
@cinime
@cinime 2 года назад
Understood! Such an amazing explanation as always, thank you very much!!
@TON-108
@TON-108 7 месяцев назад
I have done using Palindrome, class Solution { public: bool isPalindrome(vector&v) { int N = v.size(); for(int i = 0; iright); v.push_back(x->val); } else v.push_back(101); q.pop(); } if(!isPalindrome(v)) return false; } return true; } };
@aarifhannan4429
@aarifhannan4429 2 года назад
why striver repeatedly saying pre-order to in-order
@ishikapanwar8747
@ishikapanwar8747 2 года назад
I learned a lot from your graph series. This tree series is also amazing covering all important questions. Thank you! Keep posting such useful content.
@lucifersamrat6280
@lucifersamrat6280 Месяц назад
really how was his graphs series
@mriduljain1981
@mriduljain1981 Год назад
completed lecture 25 of Tree Playlist.
@neelkamalsingh6572
@neelkamalsingh6572 2 года назад
Huge Respect Sir ❤
@tech-rhino4469
@tech-rhino4469 2 года назад
💡 Mirror property is left == right and right == left 💡 pre-order traversal on root->left subtree, (root, left, right) 💡 modified pre-order traversal on root->right subtree, (root, right, left) 💡 compare the node val's if they are the same 💡 Do both traversals at the same time 💡 if left is null or right is null, then both sides must match and return true (base case)
@parthsalat
@parthsalat 2 года назад
Copy Cat
@kanikajain5627
@kanikajain5627 3 месяца назад
One way to think about this is to consider the question where we are given two binary trees and asked to tell if they are the mirror of each other or not. So we check the root (if either of them is null) and then compare their value Once done. Now , we have to check for the left and right subtrees. like the left of the first subtree val should be equal to the right of the second subtree val and vice versa. now imagine all this in a single tree. Check if the root is null first(base case and also to avoid null pointer exception) and then apply the above logic to the left and right nodes of the same tree.(considering them as different) . Thank you, Striver! Your videos have really helped me improve my thought process and intuition.
@sparshsharma6068
@sparshsharma6068 3 года назад
Understood Bhaiya🔥🔥 likeeeed, shareeeed and already subscribeeeeeeeed!!!
@AmanYadav-jf1yy
@AmanYadav-jf1yy Год назад
create two trees from given tree. T1(root->left) T2(root->right) Find inOrder traversal of both tree (T1 and T2). The given tree will be symmetric tree if inOrder traversal of T1 is equal to the reverse of inOrder traversal of T2. void inOrder(Node * root, vector &l) { if(root==nullptr) return; inOrder(root->left,l); l.push_back(root->data); inOrder(root->right,l); } bool isSymmetric(struct Node* root) { // Code here if(root==nullptr) return true; vector left,right; inOrder(root->left,left); inOrder(root->right,right); reverse(right.begin(),right.end()); return left==right; } Thank you 😍😍
@amankushwaha8180
@amankushwaha8180 Год назад
[1,2,2,2,null,2] fail for this test case
@priyanshu1919
@priyanshu1919 Год назад
@@amankushwaha8180 just push INT_MAX if null and then return , it will give correct ans
@joeljoel1236
@joeljoel1236 Год назад
@@priyanshu1919 why
@your_name96
@your_name96 2 года назад
bool isSameTree(TreeNode* root1, TreeNode* root2){ if(!root1 and !root2)return true; if(!(root1 and root2))return false; return isSameTree(root1->left, root2->right) and isSameTree(root1->right,root2->left) and root1->val == root2->val; } bool isSymmetric(TreeNode* root) { if(!root)return true; TreeNode *root1 = root->left, *root2 = root->right; return isSameTree(root1,root2); } SImple variation of is same tree problem
@Tushar-m3e3u
@Tushar-m3e3u Год назад
make it bro you are doing good job
@roushankumar7684
@roushankumar7684 8 месяцев назад
loveyou bhaiya thankyou so much for working hard for us. Can you give me advice for learning development area. I am not able to learn development!!!
@sarangtamrakar8723
@sarangtamrakar8723 2 года назад
Most of the logic is same as CHECK FOR TWO TREES ARE IDENTICAL OR NOT , JUST WE HAVE TO TAKE CARE OF MIRROR LOGIC THAT'S IT....
@JohnWick-kh7ow
@JohnWick-kh7ow 3 года назад
Why did you used dot(.) operator?
@takeUforward
@takeUforward 3 года назад
In java you can use dot in cpp its ->
@JohnWick-kh7ow
@JohnWick-kh7ow 3 года назад
@@takeUforward yes, but you used dot in cpp. That's why i asked.
@takeUforward
@takeUforward 3 года назад
@@JohnWick-kh7ow typo hogya hoga
@anuragrajput5631
@anuragrajput5631 2 года назад
ham danya huai aap ko pakar prabhu
@22_aiml_soumyonathtripathy44
@22_aiml_soumyonathtripathy44 2 года назад
You alwas mess up pre order and in order. You're saying its inorder but writing preoder.
@chethan2711
@chethan2711 2 года назад
Yaa
@tanaypatel8412
@tanaypatel8412 2 месяца назад
I solved it myself after taking in the hint form video striver you genius.
@AyushSingh-sn4te
@AyushSingh-sn4te 2 месяца назад
both are java codes
@Dontpushyour_luck
@Dontpushyour_luck 11 месяцев назад
didn't think we can solve using recursion so easily. I solved this using level order traversal btw
@knowthrvdo
@knowthrvdo 4 месяца назад
because of youre videos i am solving dsa quesion by myself without watching video.Nice lecture thank you !!!
@sangdilbiswal30
@sangdilbiswal30 3 месяца назад
I figure this out on my own in a minute. I feel level up now XD
@hydrocy.9165
@hydrocy.9165 3 месяца назад
what's point of symetrric(root->left,root->right) function?
@pilife1454
@pilife1454 9 месяцев назад
At 3:25 it's not in-order traversal it's pre-order traversal
@pilife1454
@pilife1454 9 месяцев назад
root left right is pre-order left root right is in-order
@toxic_roy
@toxic_roy Год назад
u cant use level order because it cannot differentiate between left or right for single branches
@AnuragUikey-r9q
@AnuragUikey-r9q Год назад
he killed whenever whenever
@abhaymishra7991
@abhaymishra7991 Год назад
In cpp i couldn't understand last line , why 2 rescursive relation not 1 ?
@ishanporwal4403
@ishanporwal4403 Год назад
we can also make use of the fact that inorder traversal for symmetric tree will be palindromic.
@smartswaggy6114
@smartswaggy6114 2 года назад
Check this, my approach is more intuitive than striver's void leftTree(TreeNode* root,vector&res){ if(root==NULL){ res.push_back(-1); return; } res.push_back(root->val); leftTree(root->left,res); leftTree(root->right,res); } void rightTree(TreeNode* root, vector&res){ if(root==NULL){ res.push_back(-1); return; } res.push_back(root->val); rightTree(root->right,res); rightTree(root->left,res); } bool isSymmetric(TreeNode* root) { vectorleft,right; leftTree(root,left); rightTree(root,right); return left==right; }
@amankushwaha8180
@amankushwaha8180 Год назад
leftTree(root,left); rightTree(root,right); can you explain this
@parthsalat
@parthsalat 2 года назад
Understood kaka
@anshulbhardwaj2666
@anshulbhardwaj2666 Год назад
Great video! Another solution can be that the number of nodes must be odd and the inorder traversal of the tree is always a palindrome pseudo code- 1. store inorder traversal in a vector or suitable data structure 2. if(inorder.size()%2==0) return 0 return isPalindrome(inorder)
@vm1662
@vm1662 Год назад
Hey, just sharing what I realised - I thought the same but it doesn't work always. 1 / \ 2 2 / / 2 2 The in-order traversal will give 2,2,1,2,2 and this a palindrome indeed but they are not symmetrical.
@himanshidafouty347
@himanshidafouty347 2 месяца назад
Understood
@shaiksoofi3741
@shaiksoofi3741 2 месяца назад
understood
@dreamyme543
@dreamyme543 2 года назад
Understood:)
@hareshnayak7302
@hareshnayak7302 4 месяца назад
Thanks striver for this amzing video
@thathireddyravikumar178
@thathireddyravikumar178 Год назад
Thanks a lot
@abhinanda7049
@abhinanda7049 4 месяца назад
understood
@chiragbansod8252
@chiragbansod8252 7 месяцев назад
understood
@hrishikeshbakshi8961
@hrishikeshbakshi8961 Год назад
I have a doubt, are we performing in order traversal or pre order traversal? I think the answer can be achieved by either way but the technique we are using is probably pre order traversal. Please correct me if I am wrong.
@chanchaldasnitbhopal7267
@chanchaldasnitbhopal7267 Год назад
preorder
@harshitjaiswal9439
@harshitjaiswal9439 7 месяцев назад
understood
@satyamroy3783
@satyamroy3783 Год назад
striver u 💥
@chandrachurmukherjeejucse5816
Understood.
@rakshayadav1892
@rakshayadav1892 2 года назад
Python code: class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: if not root: return if not root.left or not root.right: return root.left==root.right def helper(lroot,rroot): if not lroot or not rroot: return lroot==rroot if lroot.val!=rroot.val: return False return helper(lroot.left,rroot.right)and helper(lroot.right,rroot.left) return helper(root.left,root.right)
@supremeravi2941
@supremeravi2941 Год назад
thank u anna
@pratyushbhatt1712
@pratyushbhatt1712 Год назад
In python, Null != Null, Caution
@harshmungara9911
@harshmungara9911 Год назад
Understood
@tanishkumar6682
@tanishkumar6682 Год назад
understood
@adarshkumarrao3478
@adarshkumarrao3478 Год назад
UNDERSTOOD
@sanketh768
@sanketh768 11 месяцев назад
Crisp and clear explanation
@roopeshn3301
@roopeshn3301 2 года назад
Understood
@altamashsabri8142
@altamashsabri8142 2 года назад
Understood
@venutalla5932
@venutalla5932 Год назад
Tq sir
@pratikshadhole6694
@pratikshadhole6694 Год назад
understood. Just the time complexity should be logn instead of n cuz the skewed tree will return at first step itself
@vamsikrishnasiddu3525
@vamsikrishnasiddu3525 2 года назад
does anyone implemented this using level order traversal if yes can you please send me the psuedo code? Thanks
@cenacr007
@cenacr007 11 месяцев назад
us
@noone-nt1cp
@noone-nt1cp 5 месяцев назад
👍
@Telugu_europe
@Telugu_europe 2 года назад
US
@suvanshmahajan5902
@suvanshmahajan5902 2 года назад
"us"
@KaushikSharma-c3q
@KaushikSharma-c3q 11 месяцев назад
....
@agrawalhimanshu
@agrawalhimanshu 3 года назад
OP Bhaiya
@a3coders790
@a3coders790 2 года назад
bhai galat code hai submit nhi ho raha
@rahatsshowcase8614
@rahatsshowcase8614 2 года назад
Do a level order traversal and store each level including null ! after storing of each level check its palindrome or not ! Done
@tejas7379
@tejas7379 2 года назад
This won't work, check palindrome properties once.
@nikhilsingh5854
@nikhilsingh5854 2 года назад
@@tejas7379 It works: Check my code: class Solution: def isSymmetric(self, root: Optional[TreeNode]) -> bool: queue = deque() queue.append(root) while queue!=None: cur = [] size = len(queue) while size: size-=1 node = queue.popleft() if node==None: cur.append(None) continue cur.append(node.val) if node.left: queue.append(node.left) else: queue.append(None) if node.right: queue.append(node.right) else: queue.append(None) print(cur) if not self.isPal(cur): return False for i in cur: if i!=None: break else: return True def isPal(self,l): return l==l[::-1]
@Apoorvpandey
@Apoorvpandey 2 года назад
@@nikhilsingh5854 Your code helped!
@Vishaljoshi-uo6yc
@Vishaljoshi-uo6yc 2 года назад
space complexity bro💔💔💔💔🙂🙂🙂🙂🙂🙂
@tasneemayham974
@tasneemayham974 Год назад
AMAZING EXPLANATIONNNNSS!!!!!!!!!!!!!!!!!!!!!!
@adebisisheriff159
@adebisisheriff159 8 месяцев назад
Thanks striver!!!!..... Understood!
@yashkhatwani3198
@yashkhatwani3198 Год назад
Thank you Bhaiya , very amazing
Далее
Bearwolf - GODZILLA Пародия Beatrise
00:33
Просмотров 333 тыс.
HEAVY RANK PUSH VALORANT LIVE
Просмотров 1,2 тыс.
G-10. Rotten Oranges | C++ | Java
22:30
Просмотров 319 тыс.
Launching the best DSA Course + Platform
36:29
Просмотров 221 тыс.
Invert Binary Tree | Leetcode #226
6:39
Просмотров 56 тыс.
L11. Aestroid Collisions | Stack and Queue Playlist
17:28
Bearwolf - GODZILLA Пародия Beatrise
00:33
Просмотров 333 тыс.