LOWEST COMMON ANCESTOR OF A BINARY TREE II | PYTHON | LEETCODE 1644

Поділитися
Вставка
  • Опубліковано 4 січ 2025

КОМЕНТАРІ • 14

  • @chasing_the_horizon
    @chasing_the_horizon 2 роки тому +4

    Wow, the explanation was super clear! Thx, mate!

    • @crackfaang
      @crackfaang  2 роки тому

      Thanks! Make sure to subscribe if you haven’t already 😉

  • @symbol767
    @symbol767 2 роки тому

    Within in the first 3 minutes of your video I figured out why my code wasn't working. Once I did a check for P and for Q separately first to confirm they both exist, my solution started working.
    All I had to do is check if P or Q doesn't exist and return Null, if they both exist, then I can do the normal "find ancestor" code.
    But of course this wasn't efficient enough, so thank you for showing me the optimized version

  • @Rutik9999
    @Rutik9999 Місяць тому +1

    a bit complex, you can just find lca like (lca of BT 1) and get node1 and then perfrom node2 = lca(node1.left) or lca(node1.right)
    and if both nodes then return node1, else None

  • @FluteVJ
    @FluteVJ Рік тому

    Thank you . Your channel videos are super easy to understand.

  • @hangchen
    @hangchen Рік тому +2

    8:59 sorry I am confused here. If returning 4 or 5 doesn't matter, how would the algorithm make sure to return the correct answer, 5 but not 4?

    • @klppdc
      @klppdc 5 місяців тому +1

      Yes. This is the same question I have. Also if P and Q both are found in left subtree, why do we have to still traverse right subtree? That does not seem necessary.

    • @jayjw1
      @jayjw1 13 днів тому

      The way that the statement 'return l or r' works is it will return whichever one of those has a value attached to it. So in the example in the video (covering the example where p = 5 and q = 10), it would return the node containing the value of 5.

    • @jayjw1
      @jayjw1 13 днів тому

      @@klppdc Because it is not guaranteed that both P and Q will always be found in the left subtree. An algorithm only containing the code to find that would not pass all of the test cases that this question is asking for. So we must make sure that we also cover cases where P and Q are in separate subtrees and where P or Q (or both) do not exist.

  • @davidc4322
    @davidc4322 Рік тому

    Wouldn't storing l and r result in an O(N) space complexity? Since the None value also takes space in memory as well.

  • @jimmyahmed5424
    @jimmyahmed5424 2 роки тому

    Thank you for explaining, it was helpful!

    • @crackfaang
      @crackfaang  2 роки тому

      Glad you found the video helpful! Make sure to subscribe so you don’t miss other videos coming soon

  • @jaisoni1
    @jaisoni1 2 роки тому

    Great Explanation! Thanks!

    • @crackfaang
      @crackfaang  2 роки тому

      Glad you enjoyed the video! Make sure to subscribe so you don’t miss future videos