Binary Tree Zigzag Level Order Traversal - Leetcode 103 - Python

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

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

  • @karanmajithia
    @karanmajithia Год назад +8

    thank you for uploading the daily problems! much appreciated

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

    Thanks for immediately uploaded solution! I decided not to reverse each second list, and used stack instead. As a result got 86% by time.

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

    Nice clean and fast solution!

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

    On the line
    level = reversed(level) if len(res) % 2 else level
    you're actually appending a reverse iterator because that's what reversed() returns. You can see this if you print out `res`. You should actually call list(reversed(level)).
    LeetCode converts it to a list while checking your answer so it works on LeetCode. But it won't work in general.
    Anyway, probably a simpler way is to just reverse the level in-place. It's also more efficient.
    if len(res) % 2:
    level.reverse()

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

    Thank you for another great explanation sir !

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

    In fact when I decided to traverse every odd level in reverse order not only it was more complex it's actually not even correct
    eg: [1,2,3,4,null,null,5]
    expected : [[1] , [3,2] , [4,5]]
    output: [[[1] , [3,2] , [5,4]]

  • @JohnSmith-uu5gp
    @JohnSmith-uu5gp Год назад

    Just Awesome!!!

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

    Thank you so much

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

    Nice explanation . Could you please post the videos for the leetcode problem no 979 and 2266 ? They are interesting as well as tricky.

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

    Thanks!

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

    Thank you

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

    Leetcode102 Would be also cool to see in your explanation :)

  • @reisukami5878
    @reisukami5878 Год назад +4

    Great video as always. One question, are you going to link all these videos to your Neetcode All section of your website? It really helps me with keeping track of all the problems by category (Hashmaps, Binary Trees, Graphs, etc.) Thanks again for the content!

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

      we can do it as an audience if u want. like have a public spreadsheet and do it/maintain it for him. or ill make a website with a list and ill categorize them for everyone. could be fun. if neetcode or any of u want a solution like that, just let me know.
      neetcode is already doing so much for free. thanks for the daily.

    • @Siv-ix3id
      @Siv-ix3id Год назад

      @@uptwist2260 cool idea to make a website mate! Let me know if you're making the website, I'll contribute!!

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

    Hi neetcode! I'm pretty proud I did this one, although I didn't use the way that was intended to be the solution.
    ans = []
    def dfs(node, h):
    if not node: return
    while h >= len(ans): ans.append([])
    ans[h].append(node.val)
    dfs(node.left, h + 1)
    dfs(node.right, h + 1)
    dfs(root, 0)
    for i in range(len(ans)):
    if i % 2: ans[i].reverse()

    return ans
    Tell me what you think. Dfs is alot easier for me to implement so I always slant towards it.

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

      Nice soln keep it up !

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

      @@saifahmad141 Thank you!

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

    I tried using the logic of BFS traversal and for every alternate level I popped either from the right or the left but I got time limit exceeded error.

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

      my bad I used a list in the wrong for loop XD

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

    why not just swap the order of adding node.right and node.left depending on which level we are on for reduced space complexity

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

    Reverse in itself is O(N), how is the time complexity still O(N)?

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

      Yes even I had the same doubt?..did u got this cleared?...

    • @prwf
      @prwf 2 месяца назад +1

      For each reverse, it only reverse the number of items in that level. If you look at how many total items are being reversed, it’s roughly half of them, so O(n/2) which can simplify to O(n). So really, we’re just doing O(n) extra work so O(n)+O(n)=O(2n) = O(n).

    • @prwf
      @prwf 2 месяца назад +1

      Think about it more in terms of filling the array, which is O(n), and then performing a reversal on the entire array on after filling it. This is still O(n).

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

      @@chirpy7961look at my solution

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

    For that len(q) thing, U could have made this solution recursive to make in language independent innit?

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

    Shouldn't the time complexity be O(n^2) since half of the height of the tree your are reversing the level. It the tree is a perfect binary tree the last level is size n/2 which effectively is n. If we are reversing the nodes of each level that is odd, then at worst we get closer to O(n^2). This is my conclusion, not sure why we shouldn't take into consideration the reversal part.

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

    A totally useless optimization we can make to not have to reverse odd indexed arrays, is to use the double sided constant time addition/deletion property thing of a deque.
    Basically,
    if we are going from left -> right i.e. popping from beginning of the deque, we add the children to the end (left child first then right),
    else if we are going from right -> left i.e. popping from the end of the deque, we add the children to the start(right child first then left)
    This way we do a zig-zag bfs which kinda feels nauseating to imagine while dry running
    That way we visit each node only once, so atmost ~n operations, O(n) TC.