GOOGLE TOP INTERVIEW QUESTION 2022 | FIND LEAVES OF BINARY TREE | PYTHON - POSTORDER DFS

แชร์
ฝัง
  • เผยแพร่เมื่อ 28 ธ.ค. 2024

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

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

    MESSAGE TO FUTURE VIEWERS: Sorry it's hard to see the code for this problem. This is my very first video and I messed up the screen resolution as I still didn't know the best way to record these videos. Here is the code in case you cannot see it:
    class Solution:
    def findLeaves(self, root: Optional[TreeNode]) -> List[List[int]]:
    res = collections.defaultdict(list)
    def dfs(node, layer):
    if not node:
    return layer
    left = dfs(node.left, layer)
    right = dfs(node.right, layer)
    layer = max(left, right)
    res[layer].append(node.val)
    return layer + 1
    dfs(root, 0)
    return res.values()

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

      I just played the code part in fullscreen, its visible like that.

  • @Josh-qb4em
    @Josh-qb4em 2 ปีที่แล้ว +1

    Got this question today for my Google interview. I hadn't seen this video yet, but it was pretty easy to see it was Post-Order traversal, but when I said that, my interviewer struck me down and said it wouldn't work. He also said it would only work iteratively. So I absolutely bombed, because I couldn't think of a solution that didn't involve Post-Order traversal. He said near the end that I had to get the reverse height of the tree (so leaves are zero), and then delete the leaves that way, but iteratively. Couldn't figure it out.

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

      Wow, Google interviewers are STILL total dickheads. Honesty an interview there is either going to be an assessment of your problem solving/communication abilities or a dick measuring content where the interviewer tries to show off how smart he/she is by wanting some obscure solution. Unfortunately you got the later.
      It shouldn’t matter how you solve the question. The recursive solution isn’t exactly trivial so the fact they don’t accept it is just laughable. As long as you can explain your logic clearly and concisely and write a bug free solution, that should be enough. Go interview at Meta when the hiring freeze is over. Their bar is much more reasonable

    • @Josh-qb4em
      @Josh-qb4em 2 ปีที่แล้ว

      @@crackfaangyeah I still can’t figure out how to do this iteratively lmao. I think they were interviewing me with no intent of passing me because it was like week 6 of their two week hiring freeze. Probably would hurt their stock value if people found out it was an indefinite freeze so they kept “interviewing” people.

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

    the walkthrough helped a lot with conceptualization of your solution! thank you so much

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

      No problem! Glad you found the content useful

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

    This is amazing! Perfect walkthrough with an example!

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

      Thanks for the kind words. This was my first video and I can definitely look back at how nervous and bad at explaining things I was haha

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

    I love the layer explanation, really cool, thank you

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

    Amazing explanation, Thanks for the content .
    Here is one more easy solution :
    Idea : Group the nodes with same height
    Steps:
    1.Find the height of a given node
    2.add a simple dictionary to append the values with same height.
    Solution:
    ans=collections.defaultdict(list)
    def maxDepth(node):
    nonlocal ans
    if node==None:
    return 0
    lh=maxDepth(node.left)
    rh=maxDepth(node.right)
    height=max(lh,rh)+1
    ans[height].append(node.val)
    return height
    maxDepth(root)
    return ans

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

      Thanks! this is very intuitive

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

    great explaination with a code walk thru, you deserve move views. way too many youtubers just read the question and code it up and call it a day.

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

      Thank you for the kind words! Let me know if there’s any videos you’d like me to make and subscribe so you don’t miss future uploads

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

    Can we use a topological sorting here? I mean we can create with edges from child node to parent node and essentially do level order topological sort!
    Please correct me if I am wrong here!

  • @vikneshcs
    @vikneshcs 22 วันที่ผ่านมา

    does map preserves correct ordering

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

    Thank you for uploading this. This is amazing. Very clean and detailed the real interview-like video.

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

      I’m glad you enjoyed the video! This was my first one and definitely a bit nervous on this one haha

  • @MinhNguyen-lz1pg
    @MinhNguyen-lz1pg 2 ปีที่แล้ว +1

    great explanation!

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

    dictionaries are hash tables. isn't the return value of res.values() random?

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

      Ever since Python 3.6 all standard dictionaries are basically OrderedDicts under the hood aka they preserve insertion order so when you call .values() it should return them in the order they were added to the dictionary.

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

    Your explanation is soo clear! Thank you!

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

      Glad it was helpful!

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

    Thanks for this! Super clear explanation.

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

    super a plus explnation love it

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

      Thank you for the kind words! Let me know if there’s any videos you’d like me to make and subscribe so you don’t miss future uploads

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

    'layer' param not needed, when you reach a null node just return 0

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

    Great explaination! Thanks. Please zoom in on the code..its hard to read that.

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

      Thanks for the support! This was my first video and I messed up the resolution. I’ll post the code in the comments soon

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

    Cool 😎

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

    great explanation. clean code. Thanks a lot!

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

      Thanks for your support! Make sure to subscribe so you don’t miss future videos

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

    amazing

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

    Such an amazing explanation. Thank you!

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

      Thanks, glad you enjoyed the video

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

    Explanation of recursion is quite good...could u plz enhance the quality of the video? It's hardly visible

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

      Sorry about that, check the pinned comment for the video. I've copy pasted the code

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

    A+ explaination

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

    thank you for amazing explanation.

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

      Thanks for the kind words! Let me know if there’s any videos you’d like to see and subscribe so you don’t miss future uploads

  • @Ryan-g7h
    @Ryan-g7h 2 หลายเดือนก่อน

    nice reverse engineering solution but I don't think i can do all these in the interview lol

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

    perfect!

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

    Thanks for the video. I am curious as to how you know that this question is Google's most asked? Is there a source for frequently asked questions?

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

      Leetcode has a feature where it shows you the most frequently asked questions in interviews by company. These are user reported, though they tend to be accurate for pretty much all the top tech companies.

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

    screen is too small in leetcode page

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

      Ah yes apologies it was my first video uploaded and I recorded in 1440p instead of 1080p. When I get home from vacation I’ll post you the code

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

    I hardly can u the code in the end of the video :S sorry

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

      First video sorry, I zoom in more now