Height of Binary Tree After Subtree Removal Queries| LeetCode 2458 |must do Tree DSA | MAANG DSA

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

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

  • @randomystick
    @randomystick 28 วันที่ผ่านมา +1

    here's a version using hashmaps instead of initiating array of len 100001. it improves space complexity from beats 6% to beats 58% on leetcode:
    class Solution:
    def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]:
    res = []
    def fillHashmaps(root: Optional[TreeNode], level: int):
    if not root:
    return 0
    level_for_node[root.val] = level
    curDepth = 1 + max(fillHashmaps(root.left, level+1), fillHashmaps(root.right, level+1))
    depth_for_node[root.val] = curDepth
    if (curDepth > max_depth_for_level.get(level,0)):
    max_2nd_depth_for_level[level] = max_depth_for_level.get(level, 0)
    max_depth_for_level[level] = curDepth
    elif (curDepth > max_2nd_depth_for_level[level]):
    max_2nd_depth_for_level[level] = curDepth
    return curDepth

    level_for_node = {}
    depth_for_node = {}
    max_depth_for_level = {}
    max_2nd_depth_for_level = {}

    fillHashmaps(root, 0)
    for query in queries:
    level = level_for_node[query]
    if max_depth_for_level[level] == depth_for_node[query]:
    res.append(max_2nd_depth_for_level[level] + level - 1)
    else:
    res.append(max_depth_for_level[level] + level - 1)
    return res

    • @TheCodeThoughts
      @TheCodeThoughts  27 วันที่ผ่านมา +1

      Agree, that's a great improvement, thanks buddy

    • @randomystick
      @randomystick 27 วันที่ผ่านมา +1

      ​@@TheCodeThoughtsthank you for the clear and concise explanation!

  • @VinayListenMe
    @VinayListenMe 29 วันที่ผ่านมา

    Thanks for the explanation, it's great as using single traversal ❤❤

  • @Vinod-zs4dj
    @Vinod-zs4dj 29 วันที่ผ่านมา

  • @kanalahmet1314
    @kanalahmet1314 27 วันที่ผ่านมา

    Can you describe it how this method is going to work if the node we are going to delete according to the query is leading to the both Max_1 and Max_2 ? We can't choose Max_2 in that circumstance I think. How this gonna work then ?

    • @TheCodeThoughts
      @TheCodeThoughts  27 วันที่ผ่านมา

      Thanks for your comment, but at any level of a binary tree only one of the nodes would have a path that goes to max1 or max2......now let's say we have only one node in a level then we can delete it , it this case max2 would be zero .

  • @KATHERINESYDNEYS
    @KATHERINESYDNEYS 29 วันที่ผ่านมา +11

    I'm favoured financially with Bitcoin ETFs, Thank you buddy. $63,700 by weekly profit regardless of how bad it gets on the economy..

    • @Maryberry-b6e
      @Maryberry-b6e 29 วันที่ผ่านมา +2

      Hit $195k today. I'm really grateful for all the knowledge and nuggets you had thrown my way over the last months. Started with $14k in June 2022

    • @LucasBenjamin-n2y
      @LucasBenjamin-n2y 29 วันที่ผ่านมา +1

      Huge! been trying to trade on my own for a while now but it isn’t going well. Few weeks ago I lost about $7,000 in a particular trade. Can you at least advise me on what to do?

    • @Maryberry-b6e
      @Maryberry-b6e 29 วันที่ผ่านมา +1

      Well, I picked the challenge to put my finances in order. Then I invested in cryptocurrency, stocks, through the assistance of my discretionary fund manager, Andreas Antonopoulos

    • @GraceJones-z6h
      @GraceJones-z6h 29 วันที่ผ่านมา +1

      I'm not here to converse for him to testify just for what I'm sure of, he's trust worthy and best option ever seen..

    • @user-be4pw3xm3k
      @user-be4pw3xm3k 29 วันที่ผ่านมา +2

      Such a genuine personality!! He is really a good investment advisor. I was privileged to attend some of his seminars.
      That is how I started my crypto investment