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

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024
  • This is the 21st Video of our Binary Tree Playlist.
    In this video we will try to solve a very interesting problem asked by Google "Find Duplicate Subtrees".
    We will do live coding after explanation and see if we are able to pass all the test cases.
    Problem Name : Find Duplicate Subtrees
    Company Tags : GOOGLE
    My Github Solutions : github.com/MAZ...
    Leetcode Link : leetcode.com/p...
    My GitHub Repo for interview preparation : github.com/MAZ...
    Subscribe to my channel : / @codestorywithmik
    ╔═╦╗╔╦╗╔═╦═╦╦╦╦╗╔═╗
    ║╚╣║║║╚╣╚╣╔╣╔╣║╚╣═╣
    ╠╗║╚╝║║╠╗║╚╣║║║║║═╣
    ╚═╩══╩═╩═╩═╩╝╚╩═╩═╝
    #coding #helpajobseeker #easyrecipes #leetcode #leetcodequestionandanswers #leetcodesolution #leetcodedailychallenge #leetcodequestions #leetcodechallenge #hindi #india #hindiexplanation #hindiexplained #easyexplaination #interview #interviewtips
    #interviewpreparation #interview_ds_algo #hinglish

ความคิดเห็น • 56

  • @r.beditz3674
    @r.beditz3674 ปีที่แล้ว +15

    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 4 หลายเดือนก่อน

      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 ปีที่แล้ว +13

    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 หลายเดือนก่อน

      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.

  • @KrishKanani-ql1nq
    @KrishKanani-ql1nq ปีที่แล้ว +4

    Perfectly Explained, with nice and easy approach

  • @ravikumar-nl7zt
    @ravikumar-nl7zt หลายเดือนก่อน

    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!!

  • @krishnaneeldey8722
    @krishnaneeldey8722 ปีที่แล้ว +3

    Your explanation is the best.

  • @pritishpattnaik4674
    @pritishpattnaik4674 ปีที่แล้ว +2

    Best explanation bhaiya, the only key point in solving the tree problems is to have trust in recursion

  • @rahulmaurya6451
    @rahulmaurya6451 ปีที่แล้ว +2

    Bhai literally bhot pyara smjhaya aapne

  • @iamnoob7593
    @iamnoob7593 หลายเดือนก่อน

    Thanks good explanation

  • @thegreekgoat98
    @thegreekgoat98 ปีที่แล้ว +2

    Legend is back!

  • @Momentsofmagic28
    @Momentsofmagic28 ปีที่แล้ว +1

    Top notch explanation

  • @jogindrasingh6139
    @jogindrasingh6139 ปีที่แล้ว +1

    Nice video of today

  • @041nitinkumar7
    @041nitinkumar7 ปีที่แล้ว +1

    sbse best

  • @philosphize
    @philosphize ปีที่แล้ว +2

    Great Explanation as always

  • @vishalplayzz2580
    @vishalplayzz2580 ปีที่แล้ว +2

    tooo good asusual 🙂

  • @anshumanpatidar8599
    @anshumanpatidar8599 2 หลายเดือนก่อน +1

    Mzaa aagya .. 🤩

  • @altafmazhar7762
    @altafmazhar7762 ปีที่แล้ว +2

    The legend is back in the game.

  • @thedarkknight1865
    @thedarkknight1865 ปีที่แล้ว +1

    best explaination

  • @vamsimadugula8524
    @vamsimadugula8524 11 หลายเดือนก่อน +1

    Great Explanation

    • @codestorywithMIK
      @codestorywithMIK  11 หลายเดือนก่อน

      Thank you so much. 🙏😇

  • @adhikari669
    @adhikari669 ปีที่แล้ว

    Bhaya you are back ❤️

  • @STR09
    @STR09 ปีที่แล้ว +1

    really missed your explanations, when I got stuck in any of the daily questions.

  • @VineetKumar-pj1bk
    @VineetKumar-pj1bk ปีที่แล้ว

    As always 👏

  • @souravjoshi2293
    @souravjoshi2293 ปีที่แล้ว

    You are awesome man. Loved it

  • @shabananoor9423
    @shabananoor9423 ปีที่แล้ว

    Perfect

  • @sakshamsharma648
    @sakshamsharma648 ปีที่แล้ว

    Finally after a long time

  • @susmitjaiswal136
    @susmitjaiswal136 ปีที่แล้ว +1

    how did it handle duplicate entry in result vector

  • @riyakushwaha4347
    @riyakushwaha4347 5 หลายเดือนก่อน

    could you explain the time complexity for this

  • @soumyadipmajumdar4306
    @soumyadipmajumdar4306 4 หลายเดือนก่อน

    what is the TC?
    0(n) or o 0(n^2) because of string concatenation

  • @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  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@codestorywithMIK bhaiya ye test cases sochne aajaye toh code approach ban ne lagegi ,mujhe ye test case ki problem hori

  • @akhilesh_ku
    @akhilesh_ku หลายเดือนก่อน +1

    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😂

  • @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

  • @AnkitPandey-s9h
    @AnkitPandey-s9h ปีที่แล้ว

    bhai recursive code samjha do

  • @girikgarg8
    @girikgarg8 ปีที่แล้ว +1

    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 ปีที่แล้ว +1

      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

  • @Ash-fo4qs
    @Ash-fo4qs ปีที่แล้ว +1

    i don't understand that by only storing (pushing) the root in the vector -- how are we getting the subtree?

    • @codestorywithMIK
      @codestorywithMIK  ปีที่แล้ว +1

      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 ปีที่แล้ว +1

      @@codestorywithMIK ok got it. thanks.

  • @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 2 หลายเดือนก่อน

      @@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