Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79
@peerless35382 жыл бұрын
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 🥺
@Ayush372622 ай бұрын
@@peerless3538 Are u mad?
@peerless35382 ай бұрын
@@Ayush37262 no actually u r
@uRamPlus2 жыл бұрын
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)
@hyperme18315 ай бұрын
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
@dhruvchopra264 ай бұрын
@@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_paliwal11 ай бұрын
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.
@mohit77174 ай бұрын
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 Жыл бұрын
Completed 26/54 (48% done) !!!
@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
@lushabhsaxena66742 жыл бұрын
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 Жыл бұрын
yes, I do think the same
@kanikajain56273 ай бұрын
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-hw5jf2 күн бұрын
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.
@rishabhverma77303 жыл бұрын
So smooth so.... Lovely... Now I can overcome the Scariest fear in coding
@aakriti12 жыл бұрын
Somewhat similar to the question check whether 2 trees are identical or not with some modifications :)
@nagame859 Жыл бұрын
Absolutely!
@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.
@ishikapanwar87472 жыл бұрын
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.
@lucifersamrat62802 ай бұрын
really how was his graphs series
@thivyaamohan36712 жыл бұрын
Did anyone notice that -> should be used instead of "." in c++ code??
@amitgupta28902 жыл бұрын
and null / NULL;
@abhinavtripathi96782 жыл бұрын
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 Жыл бұрын
its java
@madgepereira289110 ай бұрын
Yep , you are right
@indubalaarora8464 ай бұрын
Does that effect @@amitgupta2890
@knowthrvdo4 ай бұрын
because of youre videos i am solving dsa quesion by myself without watching video.Nice lecture thank you !!!
@factfactorial6322 жыл бұрын
time complexity should be more accurately O(N/2) and space complexity O(N/2) because we are traveling left and right simultaneously
@tusharagarwal6155 Жыл бұрын
Doesn't matter dude...In Big-O time complexity constants are neglected anyways.
@tanaypatel84123 ай бұрын
I solved it myself after taking in the hint form video striver you genius.
@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 Жыл бұрын
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-rhino44692 жыл бұрын
💡 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)
@parthsalat2 жыл бұрын
Copy Cat
@sanketh768 Жыл бұрын
Crisp and clear explanation
@stith_pragya11 ай бұрын
Thank You So Much for this wonderful video.........🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻🙏🏻
@anishaa32987 ай бұрын
thank you so much! understood immediately!
@prabhakaran5542Ай бұрын
Understood ❤
@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-md7xr5 ай бұрын
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?
@jotsinghbindra83173 ай бұрын
@@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
@rohangangwar66043 жыл бұрын
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
@takeUforward3 жыл бұрын
Haan wo ek stretch me record krte h na and raat 3/4 baje th thoda ho jaata
@rohangangwar66043 жыл бұрын
@@takeUforward its ohk bhaiya maza aagya videos dekh ke bht acha h
@mohdhasnain38123 жыл бұрын
@@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.
@zaidachmed8682 жыл бұрын
@@mohdhasnain3812 updates hasnain?
@dikshantsharma3038 Жыл бұрын
@@zaidachmed868 no updates
@babulalyadav43054 ай бұрын
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-j8j2 жыл бұрын
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.
@ekanshsanger83562 жыл бұрын
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).
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.
@sarvottampriyadarshee54252 жыл бұрын
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
@altamashsabri81422 жыл бұрын
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)
@jayeshpaliwal22583 жыл бұрын
Understood but C++ code have Java syntax.(left.left)
@apmotivationakashparmar7224 күн бұрын
Thank you so much.
@605_samikrdas42 жыл бұрын
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-md7xr5 ай бұрын
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 Жыл бұрын
understood. Just the time complexity should be logn instead of n cuz the skewed tree will return at first step itself
@cinime2 жыл бұрын
Understood! Such an amazing explanation as always, thank you very much!!
@hareshnayak73024 ай бұрын
Thanks striver for this amzing video
@deepakojha84313 жыл бұрын
Bhaiya please playlist m sare questions cover krna jo product based companies m puchhe jate hain....
@takeUforward3 жыл бұрын
Sare hi kar rahe hai..
@deepakojha84313 жыл бұрын
@@takeUforward thank you bhaiya..... Love u ❣️❣️
@sarathchandra9412 жыл бұрын
So its a preorder traversal.
@sahulraj95367 ай бұрын
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); }
@codeman38286 ай бұрын
Understood. Very good video
@ishanporwal4403 Жыл бұрын
we can also make use of the fact that inorder traversal for symmetric tree will be palindromic.
@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 Жыл бұрын
preorder
@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 Жыл бұрын
[1,2,2,2,null,2] fail for this test case
@priyanshu1919 Жыл бұрын
@@amankushwaha8180 just push INT_MAX if null and then return , it will give correct ans
@joeljoel1236 Жыл бұрын
@@priyanshu1919 why
@himanshidafouty3473 ай бұрын
Understood
@roushankumar76849 ай бұрын
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!!!
@adebisisheriff1598 ай бұрын
Thanks striver!!!!..... Understood!
@sangdilbiswal304 ай бұрын
I figure this out on my own in a minute. I feel level up now XD
@hydrocy.91654 ай бұрын
what's point of symetrric(root->left,root->right) function?
@shaiksoofi37413 ай бұрын
understood
@toxic_roy Жыл бұрын
u cant use level order because it cannot differentiate between left or right for single branches
@judgebot7353 Жыл бұрын
perfect
@neelkamalsingh65722 жыл бұрын
Huge Respect Sir ❤
@adityan53022 жыл бұрын
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)
@devbhattacharya1532 жыл бұрын
Thanks bro
@adityan53022 жыл бұрын
@@devbhattacharya153 it's ok bro
@pilife145410 ай бұрын
At 3:25 it's not in-order traversal it's pre-order traversal
@pilife145410 ай бұрын
root left right is pre-order left root right is in-order
@sharmanihal994 ай бұрын
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
@tarunpratap39804 ай бұрын
why cant we do it like doing inordr traversal and then check for pallindrome
@rakshayadav18922 жыл бұрын
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-nt1cp6 ай бұрын
👍
@yashlakade1792 жыл бұрын
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 Жыл бұрын
Thank you sir
@Dontpushyour_luck Жыл бұрын
didn't think we can solve using recursion so easily. I solved this using level order traversal btw
@JohnWick-kh7ow3 жыл бұрын
Why did you used dot(.) operator?
@takeUforward3 жыл бұрын
In java you can use dot in cpp its ->
@JohnWick-kh7ow3 жыл бұрын
@@takeUforward yes, but you used dot in cpp. That's why i asked.
@takeUforward3 жыл бұрын
@@JohnWick-kh7ow typo hogya hoga
@Akash-Bisariya Жыл бұрын
very nicely explained!!😍😍
@mriduljain1981 Жыл бұрын
completed lecture 25 of Tree Playlist.
@abhaymishra7991 Жыл бұрын
In cpp i couldn't understand last line , why 2 rescursive relation not 1 ?
@alesblaze47452 жыл бұрын
Thanks Mate!
@iamnottech89183 ай бұрын
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); }
@ashokjena282 жыл бұрын
Thank you for this amazing series
@mutthikarunakar89512 жыл бұрын
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
@aarifhannan44292 жыл бұрын
why striver repeatedly saying pre-order to in-order
@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); } };
@bhaveshkumar68422 жыл бұрын
Immensely grateful for your content :)
@AyushSingh-sn4te3 ай бұрын
both are java codes
@tasneemayham974 Жыл бұрын
AMAZING EXPLANATIONNNNSS!!!!!!!!!!!!!!!!!!!!!!
@meetkataria29153 жыл бұрын
My approach is, if Inorder traversal of given tree is a palindrome then that tree is symmetrical. Is this correct?
@shlokdubey82203 жыл бұрын
No..
@meetkataria29153 жыл бұрын
@@shlokdubey8220 Yes you're right. I did that but code is failing for just 3 inputs on leetcode out of approx 250-280 inputs.
@shlokdubey82203 жыл бұрын
@@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.
@meetkataria29153 жыл бұрын
@@shlokdubey8220 Yeah..right
@NoThing-dc6ug3 жыл бұрын
@@meetkataria2915 How to check for how many test cases is code running correctly on leetcode?
@thathireddyravikumar178 Жыл бұрын
Thanks a lot
@sparshsharma60683 жыл бұрын
Understood Bhaiya🔥🔥 likeeeed, shareeeed and already subscribeeeeeeeed!!!
@mkitrixxmusic40232 жыл бұрын
Won't BFS work?
@yashkhatwani3198 Жыл бұрын
Thank you Bhaiya , very amazing
@ILikeItPicasso2 жыл бұрын
pre-order !in-order
@your_name962 жыл бұрын
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 Жыл бұрын
thank u anna
@rahatsshowcase86142 жыл бұрын
Do a level order traversal and store each level including null ! after storing of each level check its palindrome or not ! Done
@tejas73792 жыл бұрын
This won't work, check palindrome properties once.
@nikhilsingh58542 жыл бұрын
@@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]
@Apoorvpandey2 жыл бұрын
@@nikhilsingh5854 Your code helped!
@Vishaljoshi-uo6yc2 жыл бұрын
space complexity bro💔💔💔💔🙂🙂🙂🙂🙂🙂
@venutalla5932 Жыл бұрын
Tq sir
@harshmungara9911 Жыл бұрын
Understood
@22_aiml_soumyonathtripathy442 жыл бұрын
You alwas mess up pre order and in order. You're saying its inorder but writing preoder.
@chethan27112 жыл бұрын
Yaa
@chandrachurmukherjeejucse5816 Жыл бұрын
Understood.
@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
@neerajgarg90962 жыл бұрын
*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); } };
@Wanderingfitgeek2 жыл бұрын
your code really helped me in understanding the concept. thank you!!