Posts for: #HackerEarth

HackerEarth - Binary Tree

Solution

  • Diameter of a binary tree is maximum of diameter of current node, its left and right child.
/* Solution to HackerEarth: Binary Tree
 * URL: https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/tutorial/
 */
import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        int X = in.nextInt();
        
        Node root = new Node(X);
        
        for(int t=0; t < T-1; t++){
            String path = in.next();
            
            Node node = new Node(in.nextInt());
            
            // add new node to the tree
            add(root, path.toCharArray(), 0, node);
        }
        
        System.out.println(diameter(root));
        
    }
    
    public static void add(Node root, char[] path, int current, Node target){
        if(path.length-1 == current){
            if(path[current] == 'L'){
                // update the value of temporary node or add the new node
                if(root.left != null){
                    root.left.value = target.value;
                }
                else{
                    root.left = target;
                }
            }
            else if(path[current] == 'R'){
                // update the value of temporary node or add the new node
                if(root.right != null){
                    root.right.value = target.value;
                }
                else{
                    root.right = target;
                }
            }
        }
        else{
            if(path[current] == 'L'){
                // add a temporary node if not added yet
                if(root.left == null){
                    root.left = new Node(0);
                }
                add(root.left, path, current+1, target);
            }
            else if(path[current] == 'R'){
                // add a temporary node if not added yet
                if(root.right == null){
                    root.right = new Node(0);
                }
                add(root.right, path, current+1, target);
            }
        }
    }
    
    public static int height(Node root){
        if(root == null){
            return 0;
        }
        else{
            return Math.max(height(root.left), height(root.right))+1;
        }
    }
    
    public static int diameter(Node root){
        if(root == null){
            return 0;
        }
        else{
            // diameter of current node
            int diameter = height(root.left)+height(root.right)+1;
            
            // return maximum of diameter of current node or left child or right child            
            return Math.max(diameter, Math.max(diameter(root.left),diameter(root.right)));
        }
    }
    
}

class Node{
    public int value;
    public Node left;
    public Node right;
    
    public Node(int value){
        this.value = value;
    }
}

HackerEarth - Mirror Image

Solution

  • Parse input and build the tree by maintaining an index HashMap.
  • Traverse tree and mirror tree simultaneously to find the mirror node.
/* Solution to HackerEarth: Mirror Image
 * URL: https://www.hackerearth.com/practice/data-structures/trees/binary-and-nary-trees/practice-problems/algorithm/mirror-image-2/
 */
import java.util.*;

class TestClass {
    public static void main(String args[] ) throws Exception {
        //Scanner
        Scanner in = new Scanner(System.in);
        int N = in.nextInt();
        int Q = in.nextInt();
        
        Map<Integer, Node> index = new HashMap<>();
        
        // initialize root node
        Node root = new Node(1);
        
        index.put(1, root);
        
        // build tree
        for(int i=0; i < N-1; i++){
            int parent = in.nextInt();
            int child = in.nextInt();
            char side = in.next().charAt(0);
            
            // get parent node from index
            Node node = index.get(parent);
            
            // create a new child node and add to index
            Node childNode = new Node(child);
            index.put(child, childNode);
            
            if(side == 'L'){
                node.left = childNode;
            }
            else if (side == 'R'){
                node.right = childNode;
            }
        }
        
        // process queries
        for(int i=0; i < Q; i++){
            int target = in.nextInt();
            
            System.out.println(findMirror(root, target));
        }

    }
    
    public static int findMirror(Node root, int target){
        if(root.value == target){
            return target;
        }
        else{
            return recurse(target, root.left, root.right);
        }
    }
    
    public static int recurse(int target, Node left, Node right){

        // no need to further traverse because either current node or mirror node is null
        if(left == null || right == null){
            return -1;
        }
        
        // if target found return mirror node
        if(left.value == target){
            return right.value;
        }
        
        if(right.value == target){
            return left.value;
        }
        
        int mirror = recurse(target, left.left, right.right);
        
        // return mirror if found
        if(mirror > 0){
            return mirror;
        }
        
        return recurse(target, left.right, right.left);
    }
    
    
}

class Node {
    public int value;
    public Node left;
    public Node right;
    
    public Node(int value){
        this.value = value;
    }
}