-
Notifications
You must be signed in to change notification settings - Fork 0
/
26October2024 - 2458. Height of Binary Tree After Subtree Removal Queries.py
58 lines (51 loc) · 2.48 KB
/
26October2024 - 2458. Height of Binary Tree After Subtree Removal Queries.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def treeQueries(self, root: Optional[TreeNode], queries: List[int]) -> List[int]:
# Dictionary to store the height of each node
height = {}
# Dictionary to store the depth of each node
depth = {}
# Dictionary to store the maximum heights from each depth level
max_heights_at_depth = defaultdict(list)
# Calculate depth and height of each node using DFS
def dfs(node, d):
if not node:
return -1
depth[node.val] = d
left_height = dfs(node.left, d + 1)
right_height = dfs(node.right, d + 1)
node_height = max(left_height, right_height) + 1
height[node.val] = node_height
max_heights_at_depth[d].append(node_height)
return node_height
# Initial DFS from the root to fill depth and height info
dfs(root, 0)
# Preprocess max_heights_at_depth to store only the top two heights per depth
for d in max_heights_at_depth:
max_heights_at_depth[d].sort(reverse=True)
# Process each query to calculate the height of the tree after removal
result = []
for query in queries:
# Node's depth and height
d = depth[query]
h = height[query]
# Check the highest height after removing the subtree rooted at query
if len(max_heights_at_depth[d]) == 1:
# If there's only one height, removing it will mean height at this depth is -1
new_tree_height = depth[root.val] + max((max_heights_at_depth[dd][0] if max_heights_at_depth[dd] else -1)
for dd in max_heights_at_depth)
else:
# Otherwise, use the second-highest height in this depth level
if max_heights_at_depth[d][0] == h:
new_height = max_heights_at_depth[d][1]
else:
new_height = max_heights_at_depth[d][0]
# Compute the new height with the altered height at this level
new_tree_height = max(depth[root.val], depth[root.val] + new_height)
result.append(new_tree_height)
return result