Subtree of Another Tree (LeetCode 572) | Full solution with examples | Study Algorithms

Поделиться
HTML-код
  • Опубликовано: 10 окт 2024

Комментарии • 40

  • @unemployedcse3514
    @unemployedcse3514 3 месяца назад +4

    guys who ever is confused with '^' in Stringbuilder it is to handle below kind of tree node
    TreeNode root = new TreeNode(12);
    TreeNode subRoot = new TreeNode(2);
    where 12 (tweleve) will be treated as 1,2 subtree as 2 it gives true
    to figure out item or data in node we are using ^ as seperator
    still if u need confirmation pls refer test case created by nikhil in description code
    and nikhil small suggestion pls explain or stick to same logic during dry run , this will give more clarification and reduces doubts , in last example no null were included in ur explanation
    rest vedio is very helpful thank you ❤

  • @alisheheryar1770
    @alisheheryar1770 6 месяцев назад +6

    you have to add string("^") to your initial string otherwise it breaks at special cases like
    s = 12nullnull
    t = 2nullnull
    Cheeky Nikhil.

  • @rahulsinghdeo7519
    @rahulsinghdeo7519 Год назад +3

    Hey can you please explain how the "sb" object of String builder is preserve-ing the value when it moves to next iteration.

  • @smridhjain4100
    @smridhjain4100 4 месяца назад +1

    Whenever you put StringBuilder sb = new StringBuilder("^"); it passes the test cases but if you dont put the ^ it fails a single test case of root =[1,2] and subroot=[2] , can you explain why please? I think this is the same query that others trying to ask in the comment section

    • @nikoo28
      @nikoo28  4 месяца назад

      you have to add string("^") to your initial string otherwise it breaks at special cases like
      s = 12nullnull
      t = 2nullnull

  • @AlexTopGT
    @AlexTopGT Год назад +1

    thank you! best explanation.

  • @syedomarali2678
    @syedomarali2678 2 месяца назад

    It won't take O(n). Java's .contains() works in O(MN). Use efficient pattern-matching algorithms like KMP to achieve linear TC.

  • @minnikeswarrao6185
    @minnikeswarrao6185 Год назад +2

    Time complexity shouldn't be O(n) ...since String.contains() takes O(m+n)

    • @nikoo2805
      @nikoo2805 Год назад +1

      O(m + n) will translate to O(n), where n > m

    • @pranavm9301
      @pranavm9301 8 месяцев назад

      O(m+n) is still O(n) since m O(2n) which is worst case when n=m, which is still O(n)

  • @shrirambalaji2915
    @shrirambalaji2915 Год назад +2

    Thank you brother

  • @killeraloo3247
    @killeraloo3247 9 месяцев назад

    OP solution.
    🧡 from remote.

  • @djpsn7094
    @djpsn7094 Год назад +1

    Why we took ^ in StringBuilder

    • @nikoo28
      @nikoo28  Год назад

      it is just an anchor

  • @yemeniabedontaclf
    @yemeniabedontaclf Год назад +1

    Repeated DNA Sequences. make vedio about this

  • @pratyushtripathy1463
    @pratyushtripathy1463 7 месяцев назад

    Only pre-Order traversal will work here, In other 2 (POT & IOT) all TCs will not pass

  • @codeLine7
    @codeLine7 Год назад

    very nice explanation.

  • @garvitarora7777
    @garvitarora7777 5 месяцев назад +1

    this is failing in this test case sir
    Input
    root =[12]
    subRoot =[2]

    • @nikoo28
      @nikoo28  5 месяцев назад

      What output are you expecting?

  • @sumeersaifi6354
    @sumeersaifi6354 8 месяцев назад +1

    you code is not fully correct if there is root 12 and subroot 2 then for 12 it will be 12nullnull and for will be 2nullnull and when you check root.contain(subroot) it will give true but it should be false

    • @nikoo28
      @nikoo28  7 месяцев назад +2

      it passes the constraints on leetcode. can you explain your test case a little better?

    • @sumeersaifi6354
      @sumeersaifi6354 7 месяцев назад

      I need to check will let you know

    • @AniketWaghmare-qi7wm
      @AniketWaghmare-qi7wm Месяц назад

      Hey @nikoo28 Please Make Video On Question Number 110. Balanced Binary Tree.
      Please Make video fast because my interview in Amazon. And your teaching is so good

  • @Rieshu-l9i
    @Rieshu-l9i 6 месяцев назад

    Solve "node at distance k from target node" Please🙏

  • @sheikhmkrifat7749
    @sheikhmkrifat7749 5 месяцев назад

    i applied the same way in inorder and postorder after that it doesn't pass all the test cases in leetcode.

    • @aryavardhansingh2162
      @aryavardhansingh2162 5 месяцев назад

      same here do you know the reason ?

    • @sheikhmkrifat7749
      @sheikhmkrifat7749 5 месяцев назад

      @@aryavardhansingh2162 yeah, i found that preOrder is used for comparing structure of BT, cause here root node comes first , then left and right on the other hand inorder and post dosent follow this, for that preOrder is used to compare two BT.

  • @varundwivedi1079
    @varundwivedi1079 10 месяцев назад

    Great.Thanks

  • @blacklight8932
    @blacklight8932 Год назад +1

    didnt work for all testcases

    • @nikoo28
      @nikoo28  Год назад +1

      Check the code in description. It passes on leetcode

    • @adirang
      @adirang Месяц назад

      ​@nikoo28 The solution does not work for in order traversal when root is [1,2,3] and sub root is [1,2]. Code returns True but should be False.

  • @droid-aman
    @droid-aman 5 месяцев назад

    love ur explanion v much,, +1 suscription from my side buddy!!

  • @shahriarbadhon4596
    @shahriarbadhon4596 10 месяцев назад

    Nice

  • @BACSShaileshShettar
    @BACSShaileshShettar 7 месяцев назад

    test cases failing

    • @nikoo28
      @nikoo28  7 месяцев назад

      check the code in the github link provided in video description.

    • @adirang
      @adirang Месяц назад

      ​​​@@nikoo28The solution does not work for in order traversal when root is [1,2,3] and sub root is [1,2]. Code returns True but should be False.

    • @nikoo28
      @nikoo28  Месяц назад

      @@adirang just checked again, code is returning False.

  • @thor1626
    @thor1626 6 месяцев назад

    will fail only one test case. Anyways this method is not recommended, good explanation tho.