Cousins in Binary Tree II - Leetcode 2641 - Python

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

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

  • @__kasim__khan__
    @__kasim__khan__ หลายเดือนก่อน +16

    I was able to do this in single BFS Traversal , I just kept the childrenSum for all nodes in current level , and kept changing that after every level finishes.

  • @jamestwosheep
    @jamestwosheep หลายเดือนก่อน +1

    I like your elegant solution for this one! The solution I came up with had a single BFS pass, but involved assigning IDs to each node, using a hash map to keep track of which nodes belonged to which parent, and calculating a prefix sum and suffix sum array at each level. It turned out to be pretty efficient in terms of time and memory, but it was fairly complicated and I still feel a bit dumb for not recognising the much simpler approach in this video.

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

    can't believe i wrote the exact same code, came here to see if I did it in a stupid way or what. especially that double "if" part.
    thanks

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

    can be done in one pass
    class Solution:
    def replaceValueInTree(self, root: Optional[TreeNode]) -> Optional[TreeNode]:
    pq = deque()
    pq.append((root.val, root))
    while pq:
    n = len(pq)
    levelSum = 0
    for localSum, node in pq:
    levelSum += node.val
    for i in range(n):
    localSum, node = pq.popleft()
    childSum = 0
    if node.left: childSum += node.left.val
    if node.right: childSum += node.right.val
    if node.left: pq.append((childSum, node.left))
    if node.right: pq.append((childSum, node.right))
    node.val = levelSum - localSum
    return root

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

    as good as always! Hope you can also release a version for DFS since it will optimize the memory complexity

  • @fahimimtiaz2153
    @fahimimtiaz2153 หลายเดือนก่อน +4

    how is this man so consistent

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

      Maybe he's trying to get another job

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

      Cause he loves doing these videos, he has said multiple times. I can be consistent in drinking beer and eating chips too. But he is the GOAT thats for sure.

  • @MP-ny3ep
    @MP-ny3ep หลายเดือนก่อน +1

    Thank you so much for the daily leetcode!

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

    Came up with the same logic and mindset , but came here to optimise it further ♥

  • @vikneshcs4824
    @vikneshcs4824 หลายเดือนก่อน +1

    How this handles no cousin case

    • @SarweshParsewar
      @SarweshParsewar หลายเดือนก่อน +1

      if no cousin is present then the level sum would be the siblings sum and we are subtracting sibling sum from each child node so it becomes zero

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

    What?

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

    so fasssssssssssssssssssssssssst.

  • @parthokr-o5l
    @parthokr-o5l หลายเดือนก่อน +1

    quite alabama problem

  • @YadhuKrishna-l9g
    @YadhuKrishna-l9g หลายเดือนก่อน +1

    Bro late Today