No results found for the specified position. 2 Java Beginner Level Tree Interview Questions

MockQuestions

Java Beginner Level Tree Mock Interview

To help you prepare for your Java Beginner Level Tree interview, here are 2 interview questions and answer examples.

Get More Information About Our Java Beginner Level Tree Interview Questions

Question 1 of 2

How do you delete leaves with a given value?

This interview question concentrates on the developer's skills working with Java binary trees and iteration.

You are given a root of a binary tree and value target. Your task is to remove all the leaf nodes from the tree with a value equal to the target. One thing to note here is that if you delete a leaf node with the value target and its parent becomes a leaf node with the value target, the parent should also be removed.

/*Example*/

Consider the following tree with target = 2,
      	   1
   	 /   \	
      2    3
     /    / \
    2    2   4

After deleting all the leaf nodes with value =  2, the resultant tree becomes-
	   1
   	     \	
           3
            \
             4
Constraints:
●	The given binary tree has between 1 and 3000 nodes.

Solution:

To do the job, we need to check if a node is a leaf node and it has a value equal to the target. So we need to iterate over all the nodes. If we look carefully, the parent of some nodes should be checked after its children have been checked (that is a post-order traversal). This is because when both the children of the parent are deleted, the parent becomes a leaf and it has to be removed as well if its value is equal to the target.

public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
 
class Solution {
    public TreeNode removeLeafNodes(TreeNode root, int target) {
        if (root == null) return null;
        
        // null is returned if this node need to be removed
        root.left = removeLeafNodes(root.left, target);
        root.right = removeLeafNodes(root.right, target);
    
        // it is leaf with value equal to target, 
        // remove it, that is return null. We set the returned
        // value to parent's left or right
        if (root.left == null && root.right == null && 
root.val == target) return null;
        
        return root;
    }
    
}

Time complexity- O(n) // n is the total number of nodes
Space complexity- O(height of tree)

Written by on May 21st, 2021

Next Question

2 Java Beginner Level Tree Interview Questions & Answers

Below is a list of our Java Beginner Level Tree interview questions. Click on any interview question to view our answer advice and answer examples. You may view 5 answer examples before our paywall loads. Afterwards, you'll be asked to upgrade to view the rest of our answers.

  • 1. How do you delete leaves with a given value?

  • 2. What is the range sum of BST?