Find Duplicate Subtrees (Leetcode - 652) - (GOOGLE) : Explanation ➕ Live Coding

  Рет қаралды 8,980

codestorywithMIK

codestorywithMIK

Күн бұрын

Пікірлер: 56
@r.beditz3674
@r.beditz3674 Жыл бұрын
Great explanation bhai Below is the java code for your algorithm-: class Solution { public List findDuplicateSubtrees(TreeNode root) { List res = new ArrayList(); Map map = new HashMap(); dfs(map, root, res); return res; } private String dfs(Map map, TreeNode root, List res){ if(root == null) return "N"; String s = String.valueOf(root.val) + "," + dfs(map, root.left, res) + "," + dfs(map, root.right, res); if(map.containsKey(s) && map.get(s) == 1) res.add(root); map.put(s, map.getOrDefault(s, 0) + 1); return s; } }
@satyasanjay1339
@satyasanjay1339 5 ай бұрын
If this helps anyone. Overall algorithm is the same but implementation is slightly different. class Solution { public List findDuplicateSubtrees(TreeNode root) { List res = new ArrayList(); HashMap map = new HashMap(); String dummy = dfs(root, map); for(String key : map.keySet()){ List ls = map.get(key); if(ls.size() > 1){ res.add(ls.get(0)); } } return res; } public String dfs(TreeNode root, HashMap map){ if(root == null){ return "null"; } String key = root.val + "," + dfs(root.left, map) + "," + dfs(root.right, map); if(map.containsKey(key)){ List temp = map.get(key); temp.add(root); map.put(key, temp); } else{ List ls = new ArrayList(); ls.add(root); map.put(key, ls); } return key; } }
@krishnaradhey2814
@krishnaradhey2814 Жыл бұрын
TO think about the approach is next to impossible , if you have not solved such problem before..... moreover it feels to me like a SMART_TRICK....which you should note down in your copy......... IF YOU AGREE WITH ME THEN DO UPVOTE :-).....
@iamnoob7593
@iamnoob7593 3 ай бұрын
Yes ur right , If u dont know this problem solution , U just dont know , Practice more problems and playing on probability in interviews is the only way.
@041nitinkumar7
@041nitinkumar7 Жыл бұрын
sbse best
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot Nitin ❤️
@KrishKanani-ql1nq
@KrishKanani-ql1nq Жыл бұрын
Perfectly Explained, with nice and easy approach
@ravikumar-nl7zt
@ravikumar-nl7zt 2 ай бұрын
One important point to note is that, the order of tree traversal should match the order of string representation. Eg: if the tree traversal is post order then the string s should be string s = leftString + ',' + rightString + ',' + to_string(root->val) Messing up with the order may lead to the wrong answer. BTW, very good explanation!!
@rahulmaurya6451
@rahulmaurya6451 Жыл бұрын
Bhai literally bhot pyara smjhaya aapne
@krishnaneeldey8722
@krishnaneeldey8722 Жыл бұрын
Your explanation is the best.
@pritishpattnaik4674
@pritishpattnaik4674 Жыл бұрын
Best explanation bhaiya, the only key point in solving the tree problems is to have trust in recursion
@iamnoob7593
@iamnoob7593 3 ай бұрын
Thanks good explanation
@thekindspill
@thekindspill Жыл бұрын
Top notch explanation
@jogindrasingh6139
@jogindrasingh6139 Жыл бұрын
Nice video of today
@altafmazhar7762
@altafmazhar7762 Жыл бұрын
The legend is back in the game.
@anshumanpatidar8599
@anshumanpatidar8599 3 ай бұрын
Mzaa aagya .. 🤩
@thedarkknight1865
@thedarkknight1865 Жыл бұрын
best explaination
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thanks a lot ❤️❤️
@philosphize
@philosphize Жыл бұрын
Great Explanation as always
@vamsimadugula8524
@vamsimadugula8524 Жыл бұрын
Great Explanation
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Thank you so much. 🙏😇
@STR09
@STR09 Жыл бұрын
really missed your explanations, when I got stuck in any of the daily questions.
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
tooo good asusual 🙂
@thegreekgoat98
@thegreekgoat98 Жыл бұрын
Legend is back!
@riyakushwaha4347
@riyakushwaha4347 6 ай бұрын
could you explain the time complexity for this
@soumyadipmajumdar4306
@soumyadipmajumdar4306 6 ай бұрын
what is the TC? 0(n) or o 0(n^2) because of string concatenation
@adhikari669
@adhikari669 Жыл бұрын
Bhaya you are back ❤️
@susmitjaiswal136
@susmitjaiswal136 Жыл бұрын
how did it handle duplicate entry in result vector
@shabananoor9423
@shabananoor9423 Жыл бұрын
Perfect
@ayushjain386
@ayushjain386 Жыл бұрын
Another wonderful explanation , Like bhaiye ye approach kaise aayi string bnate hai , Like when i saw this question i used the map i was storing something like mapmpp;
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Actually i also thought this, but we can’t know from a node that other subtree values will be same right. Suppose a node value is 1 and it’s left child is 2 and right child is 3 You only store node 1 in map. But there is another subtree with node 1 and it’s left child is 4 and right child is 5 Again you store 1 in map. But these are not duplicate subtrees. So I thought that storing as string can be the only solution.
@ayushjain386
@ayushjain386 Жыл бұрын
@@codestorywithMIK bhaiya ye test cases sochne aajaye toh code approach ban ne lagegi ,mujhe ye test case ki problem hori
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
You are awesome man. Loved it
@VineetKumar-pj1bk
@VineetKumar-pj1bk Жыл бұрын
As always 👏
@neelakshisoni6118
@neelakshisoni6118 Жыл бұрын
can't I store the dircetly the value od node in arraylist and then check whether this nod eexist in arraylist , in pre order tarversal ans , output = [] , set() def dfs(node) : if not node : return if node in ans : output.add(node) else : ans.append(node) dfs(node.left) dfs(node.right) dfs(root) print(output) return list(output) but this is not giving m eteh correct answer
@madhavgupta2002
@madhavgupta2002 Жыл бұрын
if possible pls show the leetcode part in dark mode pls
@Ash-fo4qs
@Ash-fo4qs Жыл бұрын
i don't understand that by only storing (pushing) the root in the vector -- how are we getting the subtree?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
Root is a pointer to subtree. If you have the root pointer, they will traverse through it to verify the subtree
@Ash-fo4qs
@Ash-fo4qs Жыл бұрын
@@codestorywithMIK ok got it. thanks.
@sakshamsharma648
@sakshamsharma648 Жыл бұрын
Finally after a long time
@akhilesh_ku
@akhilesh_ku 2 ай бұрын
If you worried, that why should use comma so if you don't use them ,you will get wrong ans on 170th test case because it would consider 1,11,N and 11,1,N same😂
@girikgarg8
@girikgarg8 Жыл бұрын
I have a question, can we have unique representation of tree with inorder traversal? I tried, it failed, but I am not able to get why
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Yes, InOrder will fail. That's why he has used preorder (root -> left -> right)
@VineetKumar-pj1bk
@VineetKumar-pj1bk Жыл бұрын
yes but you have to use '' ,'
@girikgarg8
@girikgarg8 Жыл бұрын
@@souravjoshi2293 but why does inorder fail
@AnkitSingh-tm5dp
@AnkitSingh-tm5dp Жыл бұрын
This was happen because inorder traversal Null come first so it your substring got also also same but that time your root is null so adding a Null root in our vector ans have no sence.
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
@@AnkitSingh-tm5dp Thanks man
@AnkitPandey-s9h
@AnkitPandey-s9h Жыл бұрын
bhai recursive code samjha do
@drbullah1388
@drbullah1388 Жыл бұрын
But bhaiya, if we do in-order traversal, tab to alag honge na output trees ke?
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
Yes, InOrder will fail. That's why he has used preorder (root -> left -> right)
@Danish-saifi1
@Danish-saifi1 3 ай бұрын
@@souravjoshi2293 why its fail
@recessionriche
@recessionriche Жыл бұрын
Why not use else { mp[s]++ } Why did it work without the else?
@codestorywithMIK
@codestorywithMIK Жыл бұрын
If we get a string say “1,2,3” multiple times, we want to add it only once in our result. We only add it to result once (i.e. when initially count was 1) Now after getting the same string multiple times, we must update it’s count so that we don’t add it to result multiple times.
@souravjoshi2293
@souravjoshi2293 Жыл бұрын
I tried this too. Then I did a dry run and found out that we will add duplicate results if we add else.
@recessionriche
@recessionriche Жыл бұрын
@@codestorywithMIK ohk got it thank you very much!
@vishalplayzz2580
@vishalplayzz2580 Жыл бұрын
once we add the root into the vector mp[s] becomes 2 if it again repeats so it wont be added nxt time if the subtree repeats again
Find Duplicate Subtrees - Leetcode 652 - Python
14:33
NeetCodeIO
Рет қаралды 19 М.
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 94 МЛН
Бенчик, пора купаться! 🛁 #бенчик #арти #симбочка
00:34
Симбочка Пимпочка
Рет қаралды 3,6 МЛН
Миллионер | 2 - серия
16:04
Million Show
Рет қаралды 1,6 МЛН
А что бы ты сделал? @LimbLossBoss
00:17
История одного вокалиста
Рет қаралды 10 МЛН
Check Mirror in N-ary tree | Problem of the Day - 12/03/2022 | Yash Dwivedi
12:45
GeeksforGeeks Practice
Рет қаралды 2,1 М.
How I would learn Leetcode if I could start over
18:03
NeetCodeIO
Рет қаралды 622 М.
L52. Recover BST | Correct BST with two nodes swapped
15:56
take U forward
Рет қаралды 130 М.
ROSÉ & Bruno Mars - APT. (Official Music Video)
02:54
ROSÉ
Рет қаралды 94 МЛН