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

  Рет қаралды 158,550

take U forward

take U forward

Күн бұрын

Пікірлер: 200
@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 2 ай бұрын
​@@peerless3538 Are u mad?
@peerless3538
@peerless3538 2 ай бұрын
@@Ayush37262 no actually u r
@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 5 ай бұрын
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.
@shubham_paliwal
@shubham_paliwal 11 ай бұрын
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 4 ай бұрын
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) !!!
@shobhitsingh8695
@shobhitsingh8695 Жыл бұрын
Preorder is equal to reverse of postorder when the tree is symmetric just add some value for null when you return when reach null
@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
@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.
@JaskaranSingh-hw5jf
@JaskaranSingh-hw5jf 2 күн бұрын
easier sol: Take root->left (Invert the left subtree) apply the algo to check if root->left and root->right are same. Both these subtasks ve been covered in this playlist.
@rishabhverma7730
@rishabhverma7730 3 жыл бұрын
So smooth so.... Lovely... Now I can overcome the Scariest fear in coding
@aakriti1
@aakriti1 2 жыл бұрын
Somewhat similar to the question check whether 2 trees are identical or not with some modifications :)
@nagame859
@nagame859 Жыл бұрын
Absolutely!
@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.
@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 2 ай бұрын
really how was his graphs series
@thivyaamohan3671
@thivyaamohan3671 2 жыл бұрын
Did anyone notice that -> should be used instead of "." in c++ code??
@amitgupta2890
@amitgupta2890 2 жыл бұрын
and null / NULL;
@abhinavtripathi9678
@abhinavtripathi9678 2 жыл бұрын
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 10 ай бұрын
Yep , you are right
@indubalaarora846
@indubalaarora846 4 ай бұрын
Does that effect ​@@amitgupta2890
@knowthrvdo
@knowthrvdo 4 ай бұрын
because of youre videos i am solving dsa quesion by myself without watching video.Nice lecture thank you !!!
@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.
@tanaypatel8412
@tanaypatel8412 3 ай бұрын
I solved it myself after taking in the hint form video striver you genius.
@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.
@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
@sanketh768
@sanketh768 Жыл бұрын
Crisp and clear explanation
@stith_pragya
@stith_pragya 11 ай бұрын
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@anishaa3298
@anishaa3298 7 ай бұрын
thank you so much! understood immediately!
@prabhakaran5542
@prabhakaran5542 Ай бұрын
Understood ❤
@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 5 ай бұрын
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
@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 Жыл бұрын
@@zaidachmed868 no updates
@babulalyadav4305
@babulalyadav4305 4 ай бұрын
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.
@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)
@jayeshpaliwal2258
@jayeshpaliwal2258 3 жыл бұрын
Understood but C++ code have Java syntax.(left.left)
@apmotivationakashparmar722
@apmotivationakashparmar722 4 күн бұрын
Thank you so much.
@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 5 ай бұрын
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?
@pratikshadhole6694
@pratikshadhole6694 Жыл бұрын
understood. Just the time complexity should be logn instead of n cuz the skewed tree will return at first step itself
@cinime
@cinime 2 жыл бұрын
Understood! Such an amazing explanation as always, thank you very much!!
@hareshnayak7302
@hareshnayak7302 4 ай бұрын
Thanks striver for this amzing video
@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.
@sahulraj9536
@sahulraj9536 7 ай бұрын
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); }
@codeman3828
@codeman3828 6 ай бұрын
Understood. Very good video
@ishanporwal4403
@ishanporwal4403 Жыл бұрын
we can also make use of the fact that inorder traversal for symmetric tree will be palindromic.
@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
@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
@himanshidafouty347
@himanshidafouty347 3 ай бұрын
Understood
@roushankumar7684
@roushankumar7684 9 ай бұрын
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!!!
@adebisisheriff159
@adebisisheriff159 8 ай бұрын
Thanks striver!!!!..... Understood!
@sangdilbiswal30
@sangdilbiswal30 4 ай бұрын
I figure this out on my own in a minute. I feel level up now XD
@hydrocy.9165
@hydrocy.9165 4 ай бұрын
what's point of symetrric(root->left,root->right) function?
@shaiksoofi3741
@shaiksoofi3741 3 ай бұрын
understood
@toxic_roy
@toxic_roy Жыл бұрын
u cant use level order because it cannot differentiate between left or right for single branches
@judgebot7353
@judgebot7353 Жыл бұрын
perfect
@neelkamalsingh6572
@neelkamalsingh6572 2 жыл бұрын
Huge Respect Sir ❤
@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
@pilife1454
@pilife1454 10 ай бұрын
At 3:25 it's not in-order traversal it's pre-order traversal
@pilife1454
@pilife1454 10 ай бұрын
root left right is pre-order left root right is in-order
@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
@tarunpratap3980
@tarunpratap3980 4 ай бұрын
why cant we do it like doing inordr traversal and then check for pallindrome
@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)
@noone-nt1cp
@noone-nt1cp 6 ай бұрын
👍
@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......
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@Dontpushyour_luck
@Dontpushyour_luck Жыл бұрын
didn't think we can solve using recursion so easily. I solved this using level order traversal btw
@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
@Akash-Bisariya
@Akash-Bisariya Жыл бұрын
very nicely explained!!😍😍
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 25 of Tree Playlist.
@abhaymishra7991
@abhaymishra7991 Жыл бұрын
In cpp i couldn't understand last line , why 2 rescursive relation not 1 ?
@alesblaze4745
@alesblaze4745 2 жыл бұрын
Thanks Mate!
@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); }
@ashokjena28
@ashokjena28 2 жыл бұрын
Thank you for this amazing series
@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
@aarifhannan4429
@aarifhannan4429 2 жыл бұрын
why striver repeatedly saying pre-order to in-order
@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); } };
@bhaveshkumar6842
@bhaveshkumar6842 2 жыл бұрын
Immensely grateful for your content :)
@AyushSingh-sn4te
@AyushSingh-sn4te 3 ай бұрын
both are java codes
@tasneemayham974
@tasneemayham974 Жыл бұрын
AMAZING EXPLANATIONNNNSS!!!!!!!!!!!!!!!!!!!!!!
@meetkataria2915
@meetkataria2915 3 жыл бұрын
My approach is, if Inorder traversal of given tree is a palindrome then that tree is symmetrical. Is this correct?
@shlokdubey8220
@shlokdubey8220 3 жыл бұрын
No..
@meetkataria2915
@meetkataria2915 3 жыл бұрын
@@shlokdubey8220 Yes you're right. I did that but code is failing for just 3 inputs on leetcode out of approx 250-280 inputs.
@shlokdubey8220
@shlokdubey8220 3 жыл бұрын
@@meetkataria2915 consider a tree with 1 as root and no right child. It only has left child whose value is also 1. Inorder will be 11 which is palindrome but its not symmetric.
@meetkataria2915
@meetkataria2915 3 жыл бұрын
@@shlokdubey8220 Yeah..right
@NoThing-dc6ug
@NoThing-dc6ug 3 жыл бұрын
@@meetkataria2915 How to check for how many test cases is code running correctly on leetcode?
@thathireddyravikumar178
@thathireddyravikumar178 Жыл бұрын
Thanks a lot
@sparshsharma6068
@sparshsharma6068 3 жыл бұрын
Understood Bhaiya🔥🔥 likeeeed, shareeeed and already subscribeeeeeeeed!!!
@mkitrixxmusic4023
@mkitrixxmusic4023 2 жыл бұрын
Won't BFS work?
@yashkhatwani3198
@yashkhatwani3198 Жыл бұрын
Thank you Bhaiya , very amazing
@ILikeItPicasso
@ILikeItPicasso 2 жыл бұрын
pre-order !in-order
@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
@supremeravi2941
@supremeravi2941 Жыл бұрын
thank u anna
@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💔💔💔💔🙂🙂🙂🙂🙂🙂
@venutalla5932
@venutalla5932 Жыл бұрын
Tq sir
@harshmungara9911
@harshmungara9911 Жыл бұрын
Understood
@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
@chandrachurmukherjeejucse5816
@chandrachurmukherjeejucse5816 Жыл бұрын
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
@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!!
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
UNDERSTOOD
@nagavedareddy5891
@nagavedareddy5891 2 жыл бұрын
Huge respect...❤👏
@vaibhav9876
@vaibhav9876 2 жыл бұрын
48%done
@tanishkumar6682
@tanishkumar6682 Жыл бұрын
understood
@utkarshsharma6650
@utkarshsharma6650 2 жыл бұрын
understoood. thanks :)
@Tushar-m3e3u
@Tushar-m3e3u Жыл бұрын
make it bro you are doing good job
@priyanshumahato5270
@priyanshumahato5270 3 жыл бұрын
understood.
@ajayypalsingh
@ajayypalsingh 2 жыл бұрын
💚
@dreamyme543
@dreamyme543 2 жыл бұрын
Understood:)
@_SurajJadon_CSB
@_SurajJadon_CSB 2 жыл бұрын
Nice Video
@tanyacharanpahadi158
@tanyacharanpahadi158 3 жыл бұрын
understood!
@parthsalat
@parthsalat 2 жыл бұрын
Understood kaka
@asifiqbalsekh
@asifiqbalsekh 3 жыл бұрын
Thank You Dada...
@krishnavamsichinnapareddy
@krishnavamsichinnapareddy 2 жыл бұрын
Understood 👍
@ishaankaustav727
@ishaankaustav727 2 жыл бұрын
💚💚
@a3coders790
@a3coders790 2 жыл бұрын
bhai galat code hai submit nhi ho raha
@momilijaz271
@momilijaz271 2 жыл бұрын
done!
L26. Print Root to Node Path in Binary Tree | C++ | Java
11:00
take U forward
Рет қаралды 212 М.
L27. Lowest Common Ancestor in Binary Tree | LCA | C++ | Java
14:09
take U forward
Рет қаралды 317 М.
Friends make memories together part 2  | Trà Đặng #short #bestfriend #bff #tiktok
00:18
Synyptas 4 | Арамызда бір сатқын бар ! | 4 Bolim
17:24
黑的奸计得逞 #古风
00:24
Black and white double fury
Рет қаралды 26 МЛН
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 622 М.
L28. Maximum Width of Binary Tree | C++ | Java
22:41
take U forward
Рет қаралды 267 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 893 М.
L33. Requirements needed to construct a Unique Binary Tree | Theory
8:41
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 403 М.
How I Approach a New Leetcode Problem (live problem solving)
25:31
L15. Check for Balanced Binary Tree | C++ | Java
12:30
take U forward
Рет қаралды 345 М.
How I cracked Google Interview after 500+ rejections
8:25
Sahil & Sarra
Рет қаралды 116 М.