Similar Problems

Similar Problems not available

Find Elements In A Contaminated Binary Tree - Leetcode Solution

Companies:

LeetCode:  Find Elements In A Contaminated Binary Tree Leetcode Solution

Difficulty: Medium

Topics: hash-table depth-first-search breadth-first-search design tree binary-tree  

Problem Statement:

Given a binary tree with node values polluted by some number of contaminated nodes. Determine the original values of all the nodes in the tree.

Note: It is guaranteed that the vaccination does not affect the original structure of the tree and each node’s value is unique.

Example:

Input: 7 /
3 6 /\ /
4 5 1 2

vaccinated = [3, 5, 6]

Output: 7 /
9 6 /\ /
4 15 1 2

Approach:

The problem statement says that some of the nodes in the tree have values contaminated by vaccination and we are given the values of those contaminated nodes. We need to recover the original values of all the nodes in the binary tree.

To solve this problem we can use recursion, we can check if the current node value is in the given array of contaminated nodes then we can add the current node value plus the value of its left and right child and store it in a hashmap with the index of the node as the key because the same node can appear multiple times in the tree.

After that we can recursively call the function on both left and right child and return the node value plus the value of its left and right child if the node is not present in the hashmap.

Once we are done with the recursion we can iterate over the given array of node values and get the original value for the contaminated node by getting the value from the hashmap with the corresponding index and subtracting the value of the left and right child.

Algorithm:

  1. Create a hashmap to store the values of the nodes that are already calculated.
  2. Create a recursive function with the current node, and the given array of vaccinated nodes as arguments.
  3. If the current node is null, then return 0.
  4. Check if the value of the current node is present in the given array of vaccinated nodes. If yes, then add the value of the current node plus the value of the left and right children and store it in the hashmap using the index of the current node as the key.
  5. Recursively call the function on the left and right child of the current node.
  6. If the value of the current node is not present in the hashmap, then return the value of the current node plus the value returned from the recursive call on the left child and right child.
  7. After the recursion is complete, iterate over the given array of vaccinated nodes and use the hashmap to calculate the original value for each node.
  8. Subtract the value of the left and right child from the value stored in the hashmap for the corresponding node to get the original value.

Code:

Here is the working code for the Find Element in a Contaminated Binary Tree problem.

class Solution {
    Map<Integer, Integer> map = new HashMap<>();
    public TreeNode recoverFromVaccination(TreeNode root, int[] vaccinated) {
        recursion(root, vaccinated);
        for(int i = 0; i < vaccinated.length; i++) {
            int val = vaccinated[i];
            int sum = map.get(val);
            if(root != null) {
                int left = root.left != null ? map.getOrDefault(root.left.val, root.left.val) : 0;
                int right = root.right != null ? map.getOrDefault(root.right.val, root.right.val) : 0;
                root.val = sum - left - right;
            }
            root = root == null ? null : val == root.val ? null : root;
        }
        return root;
    }
    public int recursion(TreeNode node, int[] vaccinated) {
        if(node == null)
            return 0;
        int val = node.val;
        if(Arrays.binarySearch(vaccinated, val) >= 0)
            map.put(val, val + recursion(node.left, vaccinated) + recursion(node.right, vaccinated));
        else if(!map.containsKey(val))
            map.put(val, val + recursion(node.left, vaccinated) + recursion(node.right, vaccinated));
        return map.get(val);
    }   
}

Time Complexity: O(N)

Space Complexity: O(N)

Find Elements In A Contaminated Binary Tree Solution Code

1