JAVA program to find the lowest common ancestor of a binary tree

Posted On // Leave a Comment

CODE:

import java.util.Scanner;

public class lowestcommonAnc{
    static Scanner in =new Scanner(System.in);
   
    static node inputTree(){
        node temp=new node();
        System.out.print("Enter the value: ");
        temp.data=in.nextInt();
        System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
        if(in.next().charAt(0)=='y')
            temp.left=inputTree();
        else
            temp.left=null;
        System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
        if(in.next().charAt(0)=='y')
            temp.right=inputTree();
        else
            temp.right=null;
        return temp;
    }
 
    static void inorder(node tree){
        if(tree==null)
            return;
        inorder(tree.left);
        System.out.print(tree.data+"\t");
        inorder(tree.right);
    }

    static node lca(node tree,int n1,int n2){
        if(tree == null)
            return tree;
        if((tree.data>n1)&&(tree.data>n2))
            return lca(tree.left,n1,n2);
        if((tree.data<n1)&&(tree.data<n2))
            return lca(tree.right,n1,n2);
        return tree;
    }

    public static void main(String[] args) {
        node tree=new node();
        System.out.print("Enter y if tree is null: ");
        if(in.next().charAt(0)=='y')
            tree=null;
        else
            tree=inputTree();
        if(tree != null){
            System.out.print("Inorder traversal of the tree is:\n");
            inorder(tree);
            System.out.print("\nEnter the node 1:");
            int n1 = in.nextInt();
            System.out.print("Enter the node 2:");
            int n2 = in.nextInt();
            System.out.print("Lowest commom ancestor is:"+lca(tree,n1,n2).data);
        }
    }
}

OUTPUT:

Enter y if tree is null: n
Enter the value: 8
Enter y if the node 8 has left subtree: y
Enter the value: 3
Enter y if the node 3 has left subtree: y
Enter the value: 1
Enter y if the node 1 has left subtree: n
Enter y if the node 1 has right subtree: n
Enter y if the node 3 has right subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 6 has right subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: n
Enter y if the node 7 has right subtree: n
Enter y if the node 8 has right subtree: y
Enter the value: 10
Enter y if the node 10 has left subtree: n
Enter y if the node 10 has right subtree: y
Enter the value: 14
Enter y if the node 14 has left subtree: y
Enter the value: 13
Enter y if the node 13 has left subtree: n
Enter y if the node 13 has right subtree: n
Enter y if the node 14 has right subtree: n
Inorder traversal of the tree is:
1    3    4    6    7    8    10    13    14  
Enter the node 1:1
Enter the node 2:7
Lowest commom ancestor is:3

0 comments :

Post a Comment