LeetCode 366 | Find Leaves of Binary Tree | Solution Explained (Java + Whiteboard)

Поділитися
Вставка
  • Опубліковано 11 жов 2024
  • Given a binary tree, collect a tree's nodes as if you were doing this: Collect and remove all leaves, repeat until the tree is empty.
    Running Time: O(N)
    Space Complexity: O(N)
    Always be pluggin:
    Slack Channel: join.slack.com...
    Github: github.com/xav...
    Facebook: / xavier.hollingsworth.3
    Instagram: / xavierelon
    LinkedIn: / xavier-hollingsworth-5...
    Twitter: / elon_xavier

КОМЕНТАРІ • 17

  • @ravinsher
    @ravinsher 2 роки тому +7

    Need more white board walk through along with code.

  • @pubamx
    @pubamx 3 роки тому

    Indeed, may be a hard concept to understand, but an easy problem if you are experienced with recursion problems. I think your solution explains itself along with the whiteboard example. Thanks for sharing.

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

    I had this problem interviewed at Google back in 2021, but not binary tree, it was N-ary Tree. I was using topological sorting to resolve it.(make every node pointing to its parent node) Not at good as yours though.

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

    Great Job!! Very concise explanation....thank you.

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

    Hi Xavier, thank you for your work - keep on going! I do think though that it was worth mentioning how and way the individual levels got created in the final answer.

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

      Yes I try to address this but it is hard with recursion. I am still working on it

    • @ivaylopankov7369
      @ivaylopankov7369 4 роки тому

      The way I look at it is that the Tree levels are indices in the resulting list (Zero-based). The recursion execution ensures that a new index (new ArrayList) is added only the first time when a new level is discovered.

    • @xavierelon
      @xavierelon  4 роки тому +1

      Yes I can visualize it in my head but have trouble figuring out how to draw it out on the white board for you guys

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

    thank you !!

  • @sundixin1146
    @sundixin1146 3 роки тому +1

    holy shit I think I finally understand recursion

    • @xavierelon
      @xavierelon  3 роки тому +1

      Haha glad you figured it out. It’s a tricky topic

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

    Smart to think of it as layers and to set the not root to -1 to make the math easier to add the root values.

  • @soulboy2223
    @soulboy2223 4 роки тому +1

    thanks, but we don't remove the leaves actually, right?

    • @xavierelon
      @xavierelon  4 роки тому +1

      You can if you want but it is not required. I wrote a line in a different submission that erases the leaves and it still passes all the test cases

  • @xueli5641
    @xueli5641 3 роки тому

    It helped, thanks!

  • @carloscarrillo-calderon7862
    @carloscarrillo-calderon7862 2 роки тому +2

    Standard optimal solution, very poor explanation...