Subtree of Another Tree

แชร์
ฝัง
  • เผยแพร่เมื่อ 19 ก.ย. 2024

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

  • @KevinNaughtonJr
    @KevinNaughtonJr  4 ปีที่แล้ว +7

    DM ME YOUR FAVORITE VEGETABLE ON INSTAGRAM OR LEAVE IT IN THE COMMENTS BELOW
    10% OFF ROOFTOP SLUSHIE: bit.ly/kevinnaughton
    instagram: instagram.com/kevinnaughtonjr/
    twitter: twitter.com/kevinnaughtonjr
    patreon: www.patreon.com/KevinNaughtonJr
    merch: teespring.com/stores/kevin-naughton-jrs-store

  • @dkshadow709
    @dkshadow709 4 ปีที่แล้ว +24

    I'm a visual learner so you drawing out how we should go about solving a problem on the iPad helps. I can really picture it in my mind now. Ty for always improving for content for us =)

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

    Off the top of my head: Traverse both trees and values in two different strings respectively in preorder then Check if t string is substring of s string boom

    • @harshjoshi6257
      @harshjoshi6257 2 ปีที่แล้ว

      Pre-order traversal would not identify the tree uniquely. we would need inorder+(preorder/postorder)

  • @OverLordOfDa3rdWorld
    @OverLordOfDa3rdWorld 4 ปีที่แล้ว +15

    Hey Kevin, so glad you're still uploading during the quarantine and I hope you're doing fine. I've been working on leetcode problems and studying your solutions. I was wondering if you could suggest resources to learn traversing graphs and trees, as I feel my data structures & algorithms class did a subpar job in teaching me these fundamentals. Thank you!

    • @haaarshiiiit
      @haaarshiiiit 4 ปีที่แล้ว +6

      Here you go -
      th-cam.com/play/PLDV1Zeh2NRsDGO4--qE8yH72HFL1Km93P.html

  • @swagatochatterjee7104
    @swagatochatterjee7104 4 ปีที่แล้ว +4

    Great to have you back. I can't thank you enough. Your videos helped me a lot to land up in a tech job in a fairly reputable Indian startup.

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

    You're really natural at explaining this Kevin! Thank you for all the content you put out. This is amazing!

  • @MrSun8080
    @MrSun8080 4 ปีที่แล้ว +10

    Thanks Kevin! I'm glad you are uploading videos regularly

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

    It's failing for this test case, which must be added recently, could you please check once?
    [1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,2]
    [1,null,1,null,1,null,1,null,1,null,1,2]

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

      See the code carefully, in the else block ita subtree()||subtree. I was writing isSame in place of that

  • @leetsai6220
    @leetsai6220 4 ปีที่แล้ว

    I love popping the problem into search and finding that you made a video on it already. I know I'm in good hands at that point. These are really helpful--thanks and keep it up!

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

    what if we store inorder+(postorder/preorder) traversal of s and t in a string and check if substr(). That would be O(m)+O(N) right?

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

    Hi , I tried the same code and it is failing under one of the test cases of the leetcode ->[1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,2]
    [1,null,1,null,1,null,1,null,1,null,1,2] . you might want to take a look .

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

      did you find the fix?
      I am getting the same but not able to figure it out.

    • @narutokunnn
      @narutokunnn 2 ปีที่แล้ว

      ​@@indiansoftwareengineer4899 I assume this code worked previously when the test cases were different. But this code is not supposed to work.
      He checks first if the root == subRoot and if its not then he checks root.left or root.right. That is all he does, he never checks anything beyond that point, so if the subRoot is equal to something deeper in the root then it wont find it, hence the error you get.
      A easy way to complete the solution would be to do a BFS in the Tree first and store it in an ArrayList. Then make a for loop and check for every node in the arraylist that has same value as subRoot.val, on that node try your recusrive method to compare the nodes.

  • @valkon_
    @valkon_ 3 ปีที่แล้ว

    That case:
    [1,1]
    [1]
    Completely broke me and I need a therapist

  • @charlesbickham6604
    @charlesbickham6604 4 ปีที่แล้ว +6

    Love the new iPad layout ! Keep doin what your doin💯

  • @1247rajpatel
    @1247rajpatel 4 ปีที่แล้ว +2

    Using iPad is great for visualizing the algorithm. It is helpful. Keep using it in the future videos. Keep posting more videos. Thank you.

  • @subhambanerjee6334
    @subhambanerjee6334 4 ปีที่แล้ว +4

    I loved iPad explanations

  • @placementdork7029
    @placementdork7029 3 ปีที่แล้ว

    You are given a two-dimensional matrix with N rows and M columns. Each cell (i, j) of the matrix has some value denoted by Ai,j.
    You can do some operations on the matrix (possibly many or zero). In a single operation, you can choose any two adjacent cells and increment the value of each of them by 1. Find the minimum number of operations to make the values of all the cells of the matrix equal. If it's not possible to make all cells equal print -1.
    Input
    The first line contains two space-separated integers N and M - denoting the size of the matrix.
    Each of the next N lines contains M space-separated integers representing the values of the matrix, where the jth integer on ith line denotes the value of cell (i, j) - Ai,j.

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

    please elaborate first base case ,if(s is null) return false. why u r directly returning false . may be t is also null then its true isnt it?

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

    Pre order traversal can solve this, check if traversal of s contains traversal of t. Time and space complexity O(N + M).

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

    bro which one is better in ur opinion Ipad pro or macbook air ?
    all I want is a long battery duration and lightweight and portable and powerful ,fast and great Retinadisplay 😊

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

    What I tried to do here is I broke the problem into two parts, the first part would find me the root of the subtree from our original tree, then from that new root I called isSame function and did the sameTrees code but it failed at testcase 158/182, I think it was because due to node values were same so my code's first part would've return me the root node instead
    [1,1]
    [1]
    of its left child.

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

    Actually this can be improved by using a hashset. What you need to do is to serialise every subtree of S (including S itself)and store it in the hashset. Now all you need to do is to serialise T and check whether it exists in the hashset. Time complexity hence becomes O(|S|+|T|) with space complexity of O(|S|*|S|).

    • @jaimemiranda6520
      @jaimemiranda6520 3 ปีที่แล้ว

      Not optimal since it has to look through every possible path.
      A more optimal approach would be a preorder traversal comparison at the occurrence of the target node. In this case, the program terminates at first success.

  • @gamerhulk822
    @gamerhulk822 3 ปีที่แล้ว

    That Ipad explanation was really helpful!!!

  • @Ziggy0120
    @Ziggy0120 2 ปีที่แล้ว

    I keep overthinking these recursion and tree problems. thank you.

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

    it's of the order O(n^2). Wanted to know of order O(n) solution. Title says amazon coding interview question , definitely they will ask you to optimize it.

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

      Wouldn't it actually be O(nm) n being the original tree and m being the subtree?

    • @ajaykumarverma9285
      @ajaykumarverma9285 2 ปีที่แล้ว

      Bro search root node in main tree and from there compare two 🎄🌴

  • @jlecampana
    @jlecampana 4 ปีที่แล้ว

    I'm surprised that doing an iterative Preorder of both Trees, and then search if one of them is contained in the other is actually faster. But of course it consumes more space.

  • @darshansimha2166
    @darshansimha2166 4 ปีที่แล้ว

    If we stored the inorder traversal of Tree S as a string and Tree T as a string and see if the patters of T exists within S then we can return true, this would be of time complexity O(M) right?

  • @genghisda236
    @genghisda236 4 ปีที่แล้ว

    iPad is awesome Kevin, also you should try with apple pencil you will not be disappointed i promise. Note : you can use quicktime player on Mac to project ipad screen on mac , so u don't have to record separately and also u can explain any concept while writing the code side by side

  • @zacharysong4863
    @zacharysong4863 2 ปีที่แล้ว

    Double recursion reminds me of double rainbow lol

  • @aximilian15
    @aximilian15 4 ปีที่แล้ว

    The same problem is in cracking the coding interview and it says that the space complexity is O(log (n)+ log (m)), but it doesn't tell us why. It was solved the same why as you did, but with slightly different code.

    • @akiyoyokota8065
      @akiyoyokota8065 4 ปีที่แล้ว

      Because it takes log(n) times to look for 1 node in a tree. so when you are at any point of the tree, your call stack is of size of n.

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

    Thanks Kevin! Really happy to see you uploading videos and doing well :) I'm going to have my final interview next week in a faang company, if i get the job i'd love to support your work :)
    Cheers!

    • @darod6098
      @darod6098 4 ปีที่แล้ว

      btw: The iPad use improves a lot the learning process!

  • @aloktech1300
    @aloktech1300 4 ปีที่แล้ว

    Thank you for such awesome video. Very well explained and easy to understand.

  • @alesblaze4745
    @alesblaze4745 3 ปีที่แล้ว

    Simplicity in explanation is forcing me to like and comment.

  • @casmyu
    @casmyu 4 ปีที่แล้ว

    I am considering an extension of this question. How about if we expect Example 2 to return true? Which means we want to find out if a tree t is a piece of tree s. What would be the algorithm?

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

    Can we bring that complexity down to O(n) ?

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

    There is no fucking way I can come up with this on the spot!

  • @vk1618
    @vk1618 4 ปีที่แล้ว

    2 recursive functions, one to iterate one tree, other to compare subtree

  • @GauravBoraJodhpur
    @GauravBoraJodhpur 4 ปีที่แล้ว

    *Lovely Explanation* I was wondering if having two functions _findNode()_ and _isSame()_ improve the performance by reducing recursion stack calls?

  • @yossihekter4468
    @yossihekter4468 4 ปีที่แล้ว

    Hi Kevin,
    I really like your videos! Thank you!
    Although I think you wrong in the space complexity cause in the second function is like you said min(m, n), but in your first recursion you go trow all the tree S so I think the answer is O(m).
    If you think I'm wrong, I hope you can help me understand why
    Thank you

    • @a.yashwanth
      @a.yashwanth 4 ปีที่แล้ว +4

      The recursion stops when any node is null. The tree with less nodes will become null first, so min(m,n)

  • @hotaru6765
    @hotaru6765 2 ปีที่แล้ว

    thanks

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

    Awesome video!! Kevin what's the best way for me to level up my Java skills? I can solve Leetcode medium's in JS but I'm feeling a bit lost when it comes to Java best-practices, even on Leetcode easy's.

  • @paragroy5359
    @paragroy5359 3 ปีที่แล้ว

    Nice explanation sir

  • @prasannapm3220
    @prasannapm3220 2 ปีที่แล้ว

    amazing explanation Kevin , thanks !

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

    Amazing , please continue

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

    Is it possible that Amazon asks its O(n) version too

  • @ankitsoni9363
    @ankitsoni9363 4 ปีที่แล้ว

    Can we do inorder traversal of both and store it and then check for matching subset of elemts

    • @capoditutticapos19
      @capoditutticapos19 3 ปีที่แล้ว

      no. two trees can have same inorder traversal but diff structure

  • @mohammedghabyen721
    @mohammedghabyen721 3 ปีที่แล้ว

    Thanks a lot

  • @techronostic
    @techronostic 4 ปีที่แล้ว

    Hello Kevin, so glad to see your amazing content, can you pleaseeeee make a video series on bottom up (iterative) dynamic programming?

  • @joshuam6013
    @joshuam6013 3 ปีที่แล้ว

    dude you're awesome. What are some resources that you've used to get better at recursion?

  • @akhilchakravarthy
    @akhilchakravarthy 4 ปีที่แล้ว

    Update your Amazon Playlist man!! I solved every one of the questions feeling accomplished just to see "rotting oranges" suggested://

  • @ashwiniverma1147
    @ashwiniverma1147 3 ปีที่แล้ว

    What is time complexity?

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

    Why is one call isSubTree and the other one isSameTree?

  • @vidushidwivedi4956
    @vidushidwivedi4956 3 ปีที่แล้ว

    whats the time complexity?

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

    After video problem do look easy

  • @adityadhanrajtiwari
    @adityadhanrajtiwari 4 ปีที่แล้ว

    Hey Kevin, Why you are not travelling complete s tree , you are just checking for s,s.left,s.right node.
    subtree t can need not be only at s,s.right and s.left, it can be anywhere

    • @jovannypcg
      @jovannypcg 4 ปีที่แล้ว

      Thought the same, but then I noticed he used "isSubtree" in the last return statement of "isSubtree" (line 17), which is actually traversing the whole tree "s".

  • @sam10818
    @sam10818 3 ปีที่แล้ว

    Which monitor do you have on the side?

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

    did you cry before making this video ? haha btw, great video thank you

  • @chinmayswaroopsaini7890
    @chinmayswaroopsaini7890 4 ปีที่แล้ว

    You prove geeks can be cool too

  • @indiansoftwareengineer4899
    @indiansoftwareengineer4899 3 ปีที่แล้ว

    I liked your Ipad 4 air.
    I definitely wanted to buy that.

  • @leonine045
    @leonine045 4 ปีที่แล้ว

    Hi Kevin, I really enjoy your vids and learn a lot and I strongly believe you give a better explanation of the problems compared to others on internet.
    I was wondering if you can help me prepare for my Amazon interview (virtual/phone interview with an engineer) which is scheduled in a week! Kindly let me know. Thanks.

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

    Why wouldn’t you be trying to find the root of the sub-tree first?

  • @michaeldang8189
    @michaeldang8189 4 ปีที่แล้ว

    When Kevin said space complexity is O(min(m, n)), does he mean the call stack space? Cause I don't see any new data structure created in the code.

    • @xavierelon
      @xavierelon 4 ปีที่แล้ว

      hes referring to the recursive stack

  • @tareqdevdiary
    @tareqdevdiary 3 ปีที่แล้ว

    Thanks, man!

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

    who in the world will put this in easy

  • @joekurokawa5021
    @joekurokawa5021 3 ปีที่แล้ว

    you're a lifesaver man!

  • @nishatsayyed8326
    @nishatsayyed8326 4 ปีที่แล้ว

    A less efficient but more simpler method would be to traverse both the trees individually and append each node's value to a string. That way we'll have two strings for two trees and we can just check if one string is the "substring" of another string.

  • @sureshnandan
    @sureshnandan 4 ปีที่แล้ว

    Thank you Kevin 🤗

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

      you got it! :)

    • @sureshnandan
      @sureshnandan 4 ปีที่แล้ว

      Kevin Naughton Jr. it’s me Surendra 😁

  • @avadheshsingh4255
    @avadheshsingh4255 3 ปีที่แล้ว

    great way to explain keep going :)

  • @scnullois
    @scnullois 4 ปีที่แล้ว

    I dont get why leetcode thinks this should be False:
    [4,null,2]
    [3,4,5,null,2,null,2]
    Clearly the answer to above should be True?

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

      Coz in the second tree ,node 2 have a child node

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

    bruh... that's a nice Explaination.... Use I pad every fukin time !

  • @himanshusomani7
    @himanshusomani7 4 ปีที่แล้ว

    this came in my interview ...so pissed

  • @shrimatkapoor2200
    @shrimatkapoor2200 4 ปีที่แล้ว

    Where did you get your hoodie?

    • @KevinNaughtonJr
      @KevinNaughtonJr  4 ปีที่แล้ว

      Shrimat Kapoor teespring.com/stores/kevin-naughton-jrs-store

  • @alglaser7296
    @alglaser7296 4 ปีที่แล้ว

    Thank you always ! :D it's so much helpful

  • @Uber_handle
    @Uber_handle 3 ปีที่แล้ว

    iPad is a dope idea!

  • @alicebobson2868
    @alicebobson2868 4 ปีที่แล้ว

    loving the ipad, btw can you do question 207 course scheduler plz. thanks

  • @code7434
    @code7434 4 ปีที่แล้ว

    In isSame function , if one of s,t is NULL then in isSame function it will give runtime error. I m not satisfied with this solution

  • @ankitagarwal4914
    @ankitagarwal4914 4 ปีที่แล้ว

    Thank for sharing! I guess this approach can also be applied to 617. Merge Two Binary Trees.

  • @bhavnasharma284
    @bhavnasharma284 3 ปีที่แล้ว

    Liked the IPad :)

  • @daipayanhati2347
    @daipayanhati2347 4 ปีที่แล้ว

    Beautifulll

  • @ridhwaans
    @ridhwaans 3 ปีที่แล้ว

    is java the language you have the most proficiency, experience with? what are your other languages

  • @SreeOne
    @SreeOne 3 ปีที่แล้ว

    SubTree method and sameTree method are managed with video editing.. but nice vid though...

  • @liyuanzhang2206
    @liyuanzhang2206 2 ปีที่แล้ว

    like the ipad!

  • @eastwoodsamuel4
    @eastwoodsamuel4 4 ปีที่แล้ว

    👌👌 For the IPad

  • @garrett-ohara
    @garrett-ohara 4 ปีที่แล้ว

    I was given this testcase and it failed:
    [1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,null,1,2]
    [1,null,1,null,1,null,1,null,1,null,1,2]
    output: false, expected: true.
    Heres my code. I watched some of the video and had it a little different but using your approach, then i just straight up copied your solution and it still failed... I would like some help as to why this has happened. Thanks for the great content!
    class Solution {
    public boolean isSubtree(TreeNode s, TreeNode t) {
    if (s == null) {
    return false;
    } else if(equalTree(s,t)) {
    return true;
    } else {
    return equalTree(s.left, t) || equalTree(s.right, t);
    }
    }
    public boolean equalTree(TreeNode s, TreeNode t) {
    if (s == null || t == null) {
    return s == null && t == null;
    } else if (s.val == t.val) {
    return equalTree(s.left, t.left) && equalTree(s.right, t.right);
    } else {
    return false;
    }
    }
    }

    • @nikhilrajput5030
      @nikhilrajput5030 4 ปีที่แล้ว

      In else part you need to call isSubtree function rather than equalTrre

  • @priyanshusingh8971
    @priyanshusingh8971 3 ปีที่แล้ว

    the same code is going to give runtime error on geeksforgeeks,although explanation was very awesome

  • @ashwiniverma1147
    @ashwiniverma1147 3 ปีที่แล้ว

    What is time complexity?