height-of-binary-tree-after-subtree-removal-querie
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n\n# Complexity\n- Time 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```java []\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n *     int val;\n *     TreeNode left;\n *     TreeNode right;\n *     TreeNode() {}\n *     TreeNode(int val) { this.val = val; }\n *     TreeNode(int val, TreeNode left, TreeNode right) {\n *         this.val = val;\n *         this.left = left;\n *         this.right = right;\n *     }\n * }\n */\nclass Solution {\n        private Map<Integer, Integer> heights = new HashMap<>(); \n    private Map<Integer, Integer> depths = new HashMap<>(); \n    private Map<Integer, List<Integer>> depthHeights = new HashMap<>();\n    public int[] treeQueries(TreeNode root, int[] queries) {\n        calculateHeightAndDepth(root, 0);\n\n        for (Map.Entry<Integer, Integer> entry : heights.entrySet()) {\n            int nodeValue = entry.getKey();\n            int height = entry.getValue();\n            int depth = depths.get(nodeValue);\n            depthHeights.computeIfAbsent(depth, k -> new ArrayList<>()).add(height);\n        }\n        for (List<Integer> heightList : depthHeights.values()) {\n            Collections.sort(heightList, Collections.reverseOrder());\n        }\n\n        int[] result = new int[queries.length];\n        \n        for (int i = 0; i < queries.length; i++) {\n            int nodeValue = queries[i];\n            int depth = depths.get(nodeValue);\n            int nodeHeight = heights.get(nodeValue);\n            List<Integer> heightList = depthHeights.get(depth);\n            \n            int maxHeightWithoutNode;\n            if (heightList.size() == 1) {\n                maxHeightWithoutNode = depth - 1;\n            } else if (heightList.get(0) == nodeHeight) {\n                maxHeightWithoutNode = depth + heightList.get(1);\n            } else {\n                maxHeightWithoutNode = depth + heightList.get(0);\n            }\n\n            result[i] = maxHeightWithoutNode;\n        }\n\n        return result;\n    }\n\n    private int calculateHeightAndDepth(TreeNode node, int depth) {\n        if (node == null) return -1;\n\n        depths.put(node.val, depth);\n        int leftHeight = calculateHeightAndDepth(node.left, depth + 1);\n        int rightHeight = calculateHeightAndDepth(node.right, depth + 1);\n\n        int height = 1 + Math.max(leftHeight, rightHeight);\n        heights.put(node.val, height);\n\n        return height;\n    }\n}\n```
['Java']