L18. Check it two trees are Identical or Not | C++ | Java

  Рет қаралды 193,146

take U forward

take U forward

Күн бұрын

Пікірлер: 157
@takeUforward
@takeUforward 3 жыл бұрын
Please likeeee, shareeee and subscribeeeeeeee :) Also follow me at Insta: Striver_79
@mukeshprajapati7524
@mukeshprajapati7524 3 жыл бұрын
nxksisd
@AmitKumar-je9qo
@AmitKumar-je9qo 3 жыл бұрын
Dp plzee😑😑
@vedantjain1280
@vedantjain1280 2 жыл бұрын
my approach: if(p == NULL && q== NULL) return true; if(p == NULL || q== NULL) return false; if(p->val != q->val) return false; return isSameTree(p->right, q->right) && isSameTree(p->left, q->left) ;
@theexplorer9012
@theexplorer9012 6 ай бұрын
same bro just wrote exactly same
@whileforloops5671
@whileforloops5671 5 ай бұрын
Man the simplicity of the code is crazy awesome work dude
@rakshayadav1892
@rakshayadav1892 2 жыл бұрын
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)
@coldcoke9254
@coldcoke9254 21 күн бұрын
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.
@shivangisrivastava1158
@shivangisrivastava1158 3 жыл бұрын
always blown by your short crisp code! 👏 amazing
@the_humble_lazy
@the_humble_lazy 2 жыл бұрын
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❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤❤
@mukulupadhyay4656
@mukulupadhyay4656 2 жыл бұрын
no I don't think so
@adityasrivastava7563
@adityasrivastava7563 2 жыл бұрын
Gfg self placed course is not updated, otherwise it's not bad...
@ayushuniyal2586
@ayushuniyal2586 Жыл бұрын
agreed:)
@itikalamba3629
@itikalamba3629 Жыл бұрын
Right I had also enrolled the gfg course but still learning from strivers videos
@aadeshsharma0001
@aadeshsharma0001 3 жыл бұрын
You r my bajrangbali to my fear of trees. Thanks for this amazing tree series
@bharath890
@bharath890 2 жыл бұрын
The way you explain step by step...!!!!
@lifehustlers164
@lifehustlers164 Жыл бұрын
Completed 19/54 (35% done) !!!
@laxmipasi2059
@laxmipasi2059 5 ай бұрын
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-xv8mh
@Shubhodeep-xv8mh 4 ай бұрын
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.
@laxmipasi2059
@laxmipasi2059 4 ай бұрын
@@Shubhodeep-xv8mh didn't get can you provide psuedo code
@Shubhodeep-xv8mh
@Shubhodeep-xv8mh 4 ай бұрын
@@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
@Aryan-rh4ek Ай бұрын
@@Shubhodeep-xv8mh Thanks bro. it worked out for me
@mriduljain1981
@mriduljain1981 Жыл бұрын
completed lecture 18 of Tree playlist.
@your_name96
@your_name96 2 жыл бұрын
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;
@vikasgowdalv770
@vikasgowdalv770 3 жыл бұрын
Hey! You did almost all problems using recursion. But interviewers expect us to do in iterative manner
@sravan8643
@sravan8643 3 жыл бұрын
??
@aditya14-02
@aditya14-02 3 жыл бұрын
Bhai dekh wo iterative bhi kara rha hai aur
@gautamjh
@gautamjh 2 жыл бұрын
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; }
@amanbhadani8840
@amanbhadani8840 2 жыл бұрын
@@gautamjh Why dont You check the node while pushing in queue,whether its null or not,it may reduce your few lines of code.
@utkarshsharma6650
@utkarshsharma6650 2 жыл бұрын
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 :)
@prabhakaran5542
@prabhakaran5542 4 ай бұрын
Understood ❤
@gangsta_coder_12
@gangsta_coder_12 3 жыл бұрын
Loving your tree series very much 👌👌👌
@bhavkushwaha
@bhavkushwaha 7 ай бұрын
Thankyou Striver, Understood!
@stith_pragya
@stith_pragya Жыл бұрын
Thank You So Much for this wonderful video................🙏🏻🙏🏻🙏🏻🙏🏻
@y-be2gf
@y-be2gf 7 ай бұрын
You are explanation are very good keep making videos❤
@per.seus._
@per.seus._ Жыл бұрын
UNDERSTOOD
@cinime
@cinime 2 жыл бұрын
Understood! So smart explanation as always, thank you very much!!
@BharathS-qw9cl
@BharathS-qw9cl 6 ай бұрын
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-vn5tc
@DhananjayKumar-vn5tc 2 жыл бұрын
waiting for dp series and this playlist is very good.
@y-be2gf
@y-be2gf 7 ай бұрын
Bro you are just wow ♥️ love u brother
@surajJoshiFilms
@surajJoshiFilms 2 жыл бұрын
Space Complexity will be O(Height of Tree)?
@karthikvaradharajan6094
@karthikvaradharajan6094 2 жыл бұрын
Can we try a new approach of override equals method in Tree Node class checking data, left node and right node?
@sarthaksharma9677
@sarthaksharma9677 4 ай бұрын
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
@apmotivationakashparmar722 Ай бұрын
Thank you so much
@PrakharKulshrestha-q6e
@PrakharKulshrestha-q6e Жыл бұрын
Understood
@divyareddy7622
@divyareddy7622 2 жыл бұрын
thank you god for giving us striver
@chetanraghavv
@chetanraghavv 2 жыл бұрын
But how can we tell based on only preorder, I think different trees can have same preorder!
@Ritik-ww7ro
@Ritik-ww7ro 2 жыл бұрын
same doubt, I think we also need inorder traversal.
@solarchat9413
@solarchat9413 2 жыл бұрын
We can't
@harshitsaxena7205
@harshitsaxena7205 Жыл бұрын
@@Ritik-ww7ro have you get to know the answer then please explain
@Shivi32590
@Shivi32590 4 ай бұрын
thank you
@akshatchaube1213
@akshatchaube1213 Жыл бұрын
L 18 done
@DSAMADESIMPLE
@DSAMADESIMPLE Жыл бұрын
Amazing Striver Bhaiya
@abhinanda7049
@abhinanda7049 5 ай бұрын
understood
@harshitjaiswal9439
@harshitjaiswal9439 9 ай бұрын
understood.
@parthsalat
@parthsalat 2 жыл бұрын
Understood kaka
@vakhariyajay2224
@vakhariyajay2224 Жыл бұрын
Thank you very much. You are a genius.
@UECAshutoshKumar
@UECAshutoshKumar Жыл бұрын
Thank you sir
@androiddev884
@androiddev884 2 жыл бұрын
how is space complexity O(n)
@kireetipudi8106
@kireetipudi8106 6 ай бұрын
p = [1,1] q = [1,null,1] inorder is not working for this case
@harshitshukla1974
@harshitshukla1974 8 ай бұрын
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-levi
@manchestercity-levi 4 ай бұрын
we'll have to push the nullptr as well in the queue for proper comparison .
@adarshkumarrao3478
@adarshkumarrao3478 Жыл бұрын
UNDERSTOOD❤
@jiotv7923
@jiotv7923 2 жыл бұрын
how the space complexity is O(N)? can someone explain?
@artofwrick
@artofwrick Жыл бұрын
Recursion stack
@krishnavamsichinnapareddy
@krishnavamsichinnapareddy 2 жыл бұрын
Understood 👍
@vishalsrivastava3137
@vishalsrivastava3137 2 жыл бұрын
Your videos are always great 👍
@PalakMittal
@PalakMittal 3 жыл бұрын
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?
@codingwithanonymous890
@codingwithanonymous890 3 жыл бұрын
mine is working
@yadneshkhode3091
@yadneshkhode3091 2 жыл бұрын
aapne kidhar toh code me hagg diya hai
@Sumit-lr1qj
@Sumit-lr1qj 2 жыл бұрын
@@codingwithanonymous890 code bhej bhai
@enigmanarratives1
@enigmanarratives1 2 жыл бұрын
class Solution { public: bool help(TreeNode* p,TreeNode* q){ if(p==NULL && q==NULL) return true; if(p==NULL || q==NULL) return false; if(p->val!=q->val){ return false; } return help(p->left,q->left)&&help(p->right,q->right); } bool isSameTree(TreeNode* p, TreeNode* q) { return help(p,q); } }; its working
@suvanshmahajan5902
@suvanshmahajan5902 2 жыл бұрын
"us"
@ayah717
@ayah717 Жыл бұрын
thank you bhaiya
@NishilPatel-d2m
@NishilPatel-d2m 6 ай бұрын
if(p->val == q->val) { return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); }else return false;
@aravindmiras3708
@aravindmiras3708 6 ай бұрын
bro you drop this
@rishabhteli2339
@rishabhteli2339 2 жыл бұрын
Short videos are OP :)
@huungryyyy
@huungryyyy 4 ай бұрын
😊😊😊😊😍😍😍😍
@nishant3904
@nishant3904 2 жыл бұрын
Thank You !
@rushyya
@rushyya Жыл бұрын
UNDERSTOOD! THANK YOU🙌
@Code_Solver
@Code_Solver 3 жыл бұрын
Understood bayya
@gautamjh
@gautamjh 2 жыл бұрын
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; }
@dipanshusingh
@dipanshusingh Жыл бұрын
Loved the video. Thanks for it.❤
@engineer8340
@engineer8340 2 жыл бұрын
thankyou bhaiya
@_hulk748
@_hulk748 Жыл бұрын
Understood sir🙇‍♂❤🙏
@lavanyaprakashjampana933
@lavanyaprakashjampana933 2 жыл бұрын
we love your content and we love you..🖤
@rupampakhira8132
@rupampakhira8132 3 жыл бұрын
Understood
@snehagoyal4978
@snehagoyal4978 Жыл бұрын
bool isSameTree(TreeNode* p, TreeNode* q) { if(p==NULL && q==NULL)return true; if(!p || !q || p->val != q->val)return false; return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); }
@AbhishekKumar-td5zu
@AbhishekKumar-td5zu 3 жыл бұрын
understood❤❤
@savalalingeshreddy6750
@savalalingeshreddy6750 3 жыл бұрын
understood
@poojaroy7211
@poojaroy7211 Жыл бұрын
completed
@poetrystation3178
@poetrystation3178 4 ай бұрын
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 ??
@monismomin6759
@monismomin6759 6 ай бұрын
US!!
@nagavedareddy5891
@nagavedareddy5891 2 жыл бұрын
Huge respect...❤👏
@pritishpattnaik4674
@pritishpattnaik4674 2 жыл бұрын
So easy man
@deepakgurjar3746
@deepakgurjar3746 2 жыл бұрын
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??????
@deepakgurjar3746
@deepakgurjar3746 2 жыл бұрын
MY CODE:- class Solution { public: int solve(TreeNode* p,TreeNode* q){ if(p==NULL&& q==NULL) return 0; if(p==NULL && q!=NULL|| p!=NULL && q==NULL) return -1; if(p->val != q->val) return -1; int left= solve(p->left,q->left); if(left==-1) return -1; int right= solve(p->right,q->right); if(right==-1) return -1; return 0; } bool isSameTree(TreeNode* p, TreeNode* q) { return (solve(p,q)==0)?true:false; } };
@deepakgurjar3746
@deepakgurjar3746 2 жыл бұрын
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?
@ishwaripednekar5164
@ishwaripednekar5164 2 жыл бұрын
Amazing
@enigma2777
@enigma2777 2 жыл бұрын
Understood 🖤
@ronakslibrary8635
@ronakslibrary8635 Жыл бұрын
US
@Cool96267
@Cool96267 3 жыл бұрын
Good code
@gouravkumarshaw5467
@gouravkumarshaw5467 2 жыл бұрын
hello
@momilijaz271
@momilijaz271 2 жыл бұрын
life saver code!!!
@sahilkumarsingh8517
@sahilkumarsingh8517 3 жыл бұрын
Understood😁
@shubham6215
@shubham6215 3 жыл бұрын
Thanks 👍👍
@swapnilsrivastava9407
@swapnilsrivastava9407 3 жыл бұрын
best one
@UECSoumyaRay
@UECSoumyaRay Жыл бұрын
Keep it up
@gauravbisht8487
@gauravbisht8487 3 жыл бұрын
🌲
@utkarshsharma6650
@utkarshsharma6650 2 жыл бұрын
understooood. thanks :)
@satyamroy3783
@satyamroy3783 2 жыл бұрын
striver u rock💥
@ajayypalsingh
@ajayypalsingh 2 жыл бұрын
💚
@venkatvenkat-hj2gq
@venkatvenkat-hj2gq 2 жыл бұрын
i am loving this
@blakestudy1870
@blakestudy1870 3 жыл бұрын
nice video
@vinaygupta2369
@vinaygupta2369 Жыл бұрын
❤️❤️❤️
@ishaankaustav727
@ishaankaustav727 2 жыл бұрын
💚💚
@vani.sharmaa
@vani.sharmaa 2 жыл бұрын
us
@cenacr007
@cenacr007 Жыл бұрын
us
@vibhu613
@vibhu613 2 жыл бұрын
💖💖
@aviralsharma9260
@aviralsharma9260 Жыл бұрын
class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if (p == NULL && q == NULL) return true; else if (p == NULL || q == NULL) return false; return ((p -> val == q -> val) && isSameTree(p -> left, q -> left) && isSameTree(p-> right, q -> right)); } };
@shubamgoswami
@shubamgoswami 3 жыл бұрын
done \
@Ritik-ww7ro
@Ritik-ww7ro 2 жыл бұрын
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 ?
@abhishekpilla8857
@abhishekpilla8857 2 жыл бұрын
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
@sahilverma3976
@sahilverma3976 2 жыл бұрын
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.
@somyapratapsingh9849
@somyapratapsingh9849 2 жыл бұрын
1000th like :)
@NishilPatel-d2m
@NishilPatel-d2m 6 ай бұрын
class Solution { public: bool isSameTree(TreeNode* p, TreeNode* q) { if(p == NULL && q == NULL)return true; // base cases if(p == NULL || q == NULL)return false; if(p->val == q->val) { return isSameTree(p->left,q->left) && isSameTree(p->right,q->right); }else return false; } };
@KaushikSharma-c3q
@KaushikSharma-c3q Жыл бұрын
.
@himanshidafouty347
@himanshidafouty347 5 ай бұрын
Understood
@sarankumaar6009
@sarankumaar6009 2 жыл бұрын
"us"
@jordanpaul8697
@jordanpaul8697 3 жыл бұрын
Understood
L19. Zig-Zag or Spiral Traversal in Binary Tree | C++ | Java
8:21
take U forward
Рет қаралды 262 М.
8 patterns to solve 80% Leetcode problems
7:30
Sahil & Sarra
Рет қаралды 438 М.
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН
Муж внезапно вернулся домой @Oscar_elteacher
00:43
История одного вокалиста
Рет қаралды 6 МЛН
The IMPOSSIBLE Puzzle..
00:55
Stokes Twins
Рет қаралды 166 МЛН
L17. Maximum Path Sum in Binary Tree | C++ | Java
17:50
take U forward
Рет қаралды 362 М.
Fastest Way to Learn ANY Programming Language: 80-20 rule
8:24
Sahil & Sarra
Рет қаралды 915 М.
L15. Check for Balanced Binary Tree | C++ | Java
12:30
take U forward
Рет қаралды 357 М.
Racing Speedrunning Legend: Couriway
20:21
rekrap1
Рет қаралды 79 М.
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 673 М.
L29. Children Sum Property in Binary Tree | O(N) Approach | C++ | Java
16:13
How to STUDY so FAST it feels like CHEATING
8:03
The Angry Explainer
Рет қаралды 1,8 МЛН
I thought one thing and the truth is something else 😂
00:34
عائلة ابو رعد Abo Raad family
Рет қаралды 5 МЛН