Skip to main content

Command Palette

Search for a command to run...

Solving Leetcode Interviews in Seconds with AI: Height of Binary Tree After Subtree Removal Queries

Updated
3 min read

Introduction

In this blog post, we will explore how to solve the LeetCode problem "2458" using AI. LeetCode is a popular platform for preparing for coding interviews, and with the help of AI tools like Chatmagic, we can generate solutions quickly and efficiently - helping you pass the interviews and get the job offer without having to study for months.

Problem Statement

You are given the root of a binary tree with n nodes. Each node is assigned a unique value from 1 to n. You are also given an array queries of size m. You have to perform m independent queries on the tree where in the ith query you do the following: Remove the subtree rooted at the node with the value queries[i] from the tree. It is guaranteed that queries[i] will not be equal to the value of the root. Return an array answer of size m where answer[i] is the height of the tree after performing the ith query. Note: The queries are independent, so the tree returns to its initial state after each query. The height of a tree is the number of edges in the longest simple path from the root to some node in the tree. Example 1: Input: root = [1,3,4,2,null,6,5,null,null,null,null,null,7], queries = [4] Output: [2] Explanation: The diagram above shows the tree after removing the subtree rooted at node with value 4. The height of the tree is 2 (The path 1 -> 3 -> 2). Example 2: Input: root = [5,8,9,2,1,3,7,4,6], queries = [3,2,4,8] Output: [3,2,3,2] Explanation: We have the following queries: - Removing the subtree rooted at node with value 3. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 4). - Removing the subtree rooted at node with value 2. The height of the tree becomes 2 (The path 5 -> 8 -> 1). - Removing the subtree rooted at node with value 4. The height of the tree becomes 3 (The path 5 -> 8 -> 2 -> 6). - Removing the subtree rooted at node with value 8. The height of the tree becomes 2 (The path 5 -> 9 -> 3). Constraints: The number of nodes in the tree is n. 2 <= n <= 105 1 <= Node.val <= n All the values in the tree are unique. m == queries.length 1 <= m <= min(n, 104) 1 <= queries[i] <= n queries[i] != root.val

Explanation

Here's the breakdown of the solution:

  • Calculate Heights: Perform a depth-first search (DFS) to calculate the height of each node in the original tree. Store these heights in a dictionary or hash map for quick access.
  • Iterate Through Queries: For each query, temporarily "remove" the specified subtree by preventing the DFS height calculation from traversing into the subtree. This is done conceptually, without actually modifying the tree structure.
  • Calculate Height After Removal: After conceptually removing the subtree, run a modified DFS from the root to calculate the height of the remaining tree. Store this height as the result for the current query.

  • Time Complexity: O(m * n), where m is the number of queries and n is the number of nodes in the tree.

  • Space Complexity: O(n) for storing the height of each node and the recursion stack.

Code

    class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right

def treeQueries(root, queries):
    def calculate_heights(node, heights):
        if not node:
            return -1

        left_height = calculate_heights(node.left, heights)
        right_height = calculate_heights(node.right, heights)
        heights[node.val] = max(left_height, right_height) + 1
        return heights[node.val]

    def calculate_height_after_removal(root, removed_node_val):
        def dfs(node, depth):
            if not node:
                return depth - 1  # Return depth of parent

            if node.val == removed_node_val:
                return depth - 1 #effectively pruning this branch.

            left_height = dfs(node.left, depth + 1)
            right_height = dfs(node.right, depth + 1)
            return max(left_height, right_height)

        return dfs(root, 0)

    heights = {}
    calculate_heights(root, heights)

    result = []
    for query in queries:
        result.append(calculate_height_after_removal(root, query))

    return result

More from this blog

C

Chatmagic blog

2894 posts