Man the simplicity of the code is crazy awesome work dude
@rakshayadav18922 жыл бұрын
Python code: class Solution: def isSameTree(self, p: Optional[TreeNode], q: Optional[TreeNode]) -> bool: if not p and not q: return True if not p: return False if not q: return False if p.val!=q.val: return False return self.isSameTree(p.left,q.left) and self.isSameTree(p.right,q.right)
@coldcoke925421 күн бұрын
I don't know about others but I got confused here a little bit, and it took me quite a while to understand why we are returning like this: return (p->val == q->val) && isSameTree(p->left, q->left) && isSameTree(p->right, q->right); So let me try my best to explain to my fellow leetcoders who are also new to Binary Trees: if it is false: we return false no need to traverse the tree further. But what if they are equal we can't just return true since we need to check the tree down further so we return true and we check the left subtree and then the right subtree. Hopefully you understand now. P.S. We first the check the root node then we check the left subtree and then the right subtree similarly and when we reach null on both sides we return to the calling function.
@shivangisrivastava11583 жыл бұрын
always blown by your short crisp code! 👏 amazing
@the_humble_lazy2 жыл бұрын
honest review: the quality of striver's teaching is far far superior than gfg self paced course...........thanks a lot for what you have done for the community❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
@mukulupadhyay46562 жыл бұрын
no I don't think so
@adityasrivastava75632 жыл бұрын
Gfg self placed course is not updated, otherwise it's not bad...
@ayushuniyal2586 Жыл бұрын
agreed:)
@itikalamba3629 Жыл бұрын
Right I had also enrolled the gfg course but still learning from strivers videos
@aadeshsharma00013 жыл бұрын
You r my bajrangbali to my fear of trees. Thanks for this amazing tree series
@bharath8902 жыл бұрын
The way you explain step by step...!!!!
@lifehustlers164 Жыл бұрын
Completed 19/54 (35% done) !!!
@laxmipasi20595 ай бұрын
if we are using any traversal some cases are failing because for few case will get same traversal values. eg. p =[1,1] q = [1,null,1] so I don't think we can use traversal
@Shubhodeep-xv8mh4 ай бұрын
Push some arbitrary constant value(out of node->val constraints, for example INT_MIN or INT_MAX) in the vector whenever you encounter a null node while traversal.
@laxmipasi20594 ай бұрын
@@Shubhodeep-xv8mh didn't get can you provide psuedo code
@Shubhodeep-xv8mh4 ай бұрын
@@laxmipasi2059 In the preorder traversal function, for condition of null nodes, add - if (node == NULL) { pre.push_back(INT_MIN); // arbitrary value lesser than min possible node->val return; } In your example in the above comment, whenever we encounter a null left node as in q, we push this constant INT_MIN in the vector which helps us differentiate it from p where the right node is null.
@Aryan-rh4ekАй бұрын
@@Shubhodeep-xv8mh Thanks bro. it worked out for me
@mriduljain1981 Жыл бұрын
completed lecture 18 of Tree playlist.
@your_name962 жыл бұрын
my code: if(!p and !q)return true; // both null if(!(p and q))return false; // if one null and other not null return sol(p->left,q->left) and sol(p->right,q->right) and p->val == q->val;
@vikasgowdalv7703 жыл бұрын
Hey! You did almost all problems using recursion. But interviewers expect us to do in iterative manner
@sravan86433 жыл бұрын
??
@aditya14-023 жыл бұрын
Bhai dekh wo iterative bhi kara rha hai aur
@gautamjh2 жыл бұрын
Iterative Solution using queue : bool isSameTree(TreeNode* p, TreeNode* q) { queue Q; if(!p or !q) return p==q; //If both roots are null Q.push(p); Q.push(q); //Push both the roots in the queue while(!Q.empty()) { TreeNode* left = Q.front(); Q.pop(); //Store one node in left and pop TreeNode *right = Q.front(); Q.pop(); //Store other in right and pop if(!left and !right) continue; //If both nodes are null -> continue if(!left or !right) return false; //If one of them is null, simply return false if(left->val != right->val) return false; //If they are not equal, return false //Push left childs of both nodes Q.push(left->left); Q.push(right->left); //Push right child of both nodes Q.push(left->right); Q.push(right->right); } return true; }
@amanbhadani88402 жыл бұрын
@@gautamjh Why dont You check the node while pushing in queue,whether its null or not,it may reduce your few lines of code.
@utkarshsharma66502 жыл бұрын
what's the difference? logic is similar, it's just that the iteration is done using a queue. rest, the conditions, the logic remains the same, buddy :)
@prabhakaran55424 ай бұрын
Understood ❤
@gangsta_coder_123 жыл бұрын
Loving your tree series very much 👌👌👌
@bhavkushwaha7 ай бұрын
Thankyou Striver, Understood!
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video................🙏🏻🙏🏻🙏🏻🙏🏻
@y-be2gf7 ай бұрын
You are explanation are very good keep making videos❤
@per.seus._ Жыл бұрын
UNDERSTOOD
@cinime2 жыл бұрын
Understood! So smart explanation as always, thank you very much!!
@BharathS-qw9cl6 ай бұрын
Bhaiyya, as u said that recursion could be optimised by stopping any further calls when any one the calls return true, I tried out this one here class Solution { public: bool checkusingpreorder(TreeNode* p, TreeNode* q){ if(p == NULL && q == NULL) return true; if(p == NULL || q == NULL || p -> val != q -> val) return false; if(checkusingpreorder(p->left,q->left) == false) return false; if(checkusingpreorder(p->right,q->right) == false) return false; return true; } bool isSameTree(TreeNode* p, TreeNode* q) { return checkusingpreorder(p,q); } };
@DhananjayKumar-vn5tc2 жыл бұрын
waiting for dp series and this playlist is very good.
@y-be2gf7 ай бұрын
Bro you are just wow ♥️ love u brother
@surajJoshiFilms2 жыл бұрын
Space Complexity will be O(Height of Tree)?
@karthikvaradharajan60942 жыл бұрын
Can we try a new approach of override equals method in Tree Node class checking data, left node and right node?
@sarthaksharma96774 ай бұрын
Hey Striver, had a quick query What if I have a vector and push_back some grabage value like 0.001 into it when nullptr is encountered and then compare thiese vectors for both, MY DOUBT IS this approach works for pre order but not for inorder, I am not able to think of a case in which pre order would fail but logically I feel there could exist a case where this fails like inorder traversal and am not able to justify that preorder would be correct conceptually.
@apmotivationakashparmar722Ай бұрын
Thank you so much
@PrakharKulshrestha-q6e Жыл бұрын
Understood
@divyareddy76222 жыл бұрын
thank you god for giving us striver
@chetanraghavv2 жыл бұрын
But how can we tell based on only preorder, I think different trees can have same preorder!
@Ritik-ww7ro2 жыл бұрын
same doubt, I think we also need inorder traversal.
@solarchat94132 жыл бұрын
We can't
@harshitsaxena7205 Жыл бұрын
@@Ritik-ww7ro have you get to know the answer then please explain
@Shivi325904 ай бұрын
thank you
@akshatchaube1213 Жыл бұрын
L 18 done
@DSAMADESIMPLE Жыл бұрын
Amazing Striver Bhaiya
@abhinanda70495 ай бұрын
understood
@harshitjaiswal94399 ай бұрын
understood.
@parthsalat2 жыл бұрын
Understood kaka
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@UECAshutoshKumar Жыл бұрын
Thank you sir
@androiddev8842 жыл бұрын
how is space complexity O(n)
@kireetipudi81066 ай бұрын
p = [1,1] q = [1,null,1] inorder is not working for this case
@harshitshukla19748 ай бұрын
at 3:38 you said that we can do it with level order traversal. how cane we do this with level order traversal. -> suppose we have two trees (both of 2 nodes only) where root of both trees is 1 and in the first tree we have left child as 2 and in the second tree we have right child as 2. In this case the level order traversal will result in saying that they are same but actually they are not. Please anyone tell me am I missing something in the level order traversal?
@manchestercity-levi4 ай бұрын
we'll have to push the nullptr as well in the queue for proper comparison .
@adarshkumarrao3478 Жыл бұрын
UNDERSTOOD❤
@jiotv79232 жыл бұрын
how the space complexity is O(N)? can someone explain?
@artofwrick Жыл бұрын
Recursion stack
@krishnavamsichinnapareddy2 жыл бұрын
Understood 👍
@vishalsrivastava31372 жыл бұрын
Your videos are always great 👍
@PalakMittal3 жыл бұрын
Why preorder, inorder and postorder are giving error if there is a null node in between, and level order traversal or bfs is working fine?
Iterative Solution using queue : bool isSameTree(TreeNode* p, TreeNode* q) { queue Q; if(!p or !q) return p==q; //If both roots are null Q.push(p); Q.push(q); //Push both the roots in the queue while(!Q.empty()) { TreeNode* left = Q.front(); Q.pop(); //Store one node in left and pop TreeNode *right = Q.front(); Q.pop(); //Store other in right and pop if(!left and !right) continue; //If both nodes are null -> continue if(!left or !right) return false; //If one of them is null, simply return false if(left->val != right->val) return false; //If they are not equal, return false //Push left childs of both nodes Q.push(left->left); Q.push(right->left); //Push right child of both nodes Q.push(left->right); Q.push(right->right); } return true; }
why everyone is appreciating even though his code is passing on 2 test case in gfg ?? honestly i have been following his video but koi bhi code run nhi hota hai chatgpt se 10 baar correct krva kr run hota hai in sir ki itni hype q hai market mei ??
@monismomin67596 ай бұрын
US!!
@nagavedareddy58912 жыл бұрын
Huge respect...❤👏
@pritishpattnaik46742 жыл бұрын
So easy man
@deepakgurjar37462 жыл бұрын
How can we directly compare p==q isn't there refrences are different in memory, for that we have to compare there value/data at each node????? bcz this same concept of refrences earlier used in video of problem: Intersection Point in Y shaped linked list I done according to that and my soln is accepted but i also want to clear my concept,bhaiya plz replyyyy??????
i get a little clarity that we are comparinthem only when one of them is null and in this case one is null and other is not null then we get our ans....also if both are null then they have same refrences i guess that why our solution is working perfectly fine for all cases?
how can we tell based on only preorder, as 2 trees can have same preorder . I think we also need to check inorder traversal. If anyone can clear ?
@abhishekpilla88572 жыл бұрын
Even I have the same doubt 2 Trees can have pre orders traversal same...either we have to to 2 traversals and one should be inorder
@sahilverma39762 жыл бұрын
The code still works because we're checking the traversals of both the trees simultaneously. Take two different binary trees with the same preorder traversal, dry run the code and you'll get the idea about what I'm saying.