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 ?
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 .
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?
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
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
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
Agree, that's a great improvement, thanks buddy
@@TheCodeThoughtsthank you for the clear and concise explanation!
Thanks for the explanation, it's great as using single traversal ❤❤
Glad you like it!
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 ?
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 .
I'm favoured financially with Bitcoin ETFs, Thank you buddy. $63,700 by weekly profit regardless of how bad it gets on the economy..
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
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?
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
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..
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