Finding the Least Common Ancestor of a Binary Tree Input a binary tree (the root node) and 2 other nodes that need to be assessed for finding the least common ancestor (LCA)
Code:
a. Finding the nodes first with either breadthFirstSearch or depthFirstSearch
b. If they both exist, find the LCA.
What searching mechanism would be best here? Using breadthFirst for now.




    import java.util.LinkedList;
    import java.util.Queue;
    import java.util.Stack;

    class Treenode {
      public int value;
      public Treenode right;
      public Treenode left;
      public Treenode(int value) {
        this.value = value;
        this.left = null;
        this.right = null;
    }
    }
    public class SearchTree {
     static boolean v1=false,v2=false;
    public static void main(String[] args) {
    Treenode n1 = new Treenode(5);
    Treenode n2 = new Treenode(2);
    Treenode n3 = new Treenode(4);
    Treenode n4 = new Treenode(1);
    Treenode n5 = new Treenode(3);
    n1.left = n2;
    n1.right = n3;
    n2.left = n4;
    n2.right = n5;
    System.out.println("Found BFS? :"+ breadthFirstSearch(n1, 14));

    System.out.println("Found DFS? :"+ depthFirstSearch(n1, 5));
    System.out.println();

    System.out.println("LCA "+" is "+lca(n1,5,50));

    }
    public static boolean breadthFirstSearch(Treenode root, int value){
    if(root==null){
        return false;
    }
    Queue&lt;Treenode&gt; q1 = new LinkedList&lt;Treenode&gt;();
    q1.add(root);
    while(!q1.isEmpty())
    {
        Treenode temp = q1.peek();

        if(temp!=null) {
            q1.remove();
            if (temp.value == value) return true;
            if (temp.left != null) q1.add(temp.left);
            if (temp.right != null) q1.add(temp.right);
        }
    }
    return false;
    }
    public static boolean depthFirstSearch(Treenode root, int value){
    if(root==null){
        return false;
    }
    Stack&lt;Treenode&gt; s1 = new Stack&lt;Treenode&gt;();
    s1.add(root);

    while(!s1.isEmpty()){
       Treenode temp = s1.peek();
        if(temp!=null){
            s1.pop();
            if (temp.value == value) return true;
            if (temp.right != null) s1.push(temp.right);
            if (temp.left != null) s1.push(temp.left);

        }
        }
        return false;
        }

    public static Treenode lcaHelper(Treenode head, int x,int y){

    if(head==null){
        return null;
    }

    if(head.value == x || head.value ==y){
        if (head.value == y){
            v2 = true;
            return head;
        }
        else {
            v1 = true;
            return head;
        }
    }

    Treenode left = lcaHelper(head.left, x, y);
    Treenode right = lcaHelper(head.right,x,y);

    if(left!=null &amp;&amp; right!=null){
        return head;
    }
    return (left!=null) ? left:right;
    }


    public static int lca(Treenode head, int h1, int h2) {
    v1 = breadthFirstSearch(head, h1);
    v2 = breadthFirstSearch(head,h2);
    if(v1 &amp;&amp; v2){
        Treenode lca = lcaHelper(head,h1,h2);
        return lca.value;
    }
    return -1;
    }
    }
Introduction

I noticed that your demo binary tree is not sorted by values, so I assume that we are dealing with the unsorted version of the data structure.

Default values for reference fields

public Treenode(int value) {
    this.value = value;
    this.left = null;
    this.right = null;
}


In Java, the reference fields are initialized with the value null by default. You can simply write

public Treenode(int value) {
    this.value = value;
}


Actual algorithms

Your BFS and DFS seem like overkilling it. See Summa summarum for an alternative implementation.

Static vs. non-static

static boolean v1=false,v2=false;


Not good. Consider creating the two variables in the actual LCA routine.

Whitespace

Once again:

static boolean v1=false,v2=false;


You should have a single space before and after each binary operator:

static boolean v1 = false, v2 = false;


Hide the object

Since SearchTree is useless as an object, consider declaring a private constructor.

Summa summarum

All in all, I had this in mind:

Treenode.java:

public class Treenode {

    private final int value;
    private Treenode right;
    private Treenode left;

    public Treenode(final int value) {
        this.value = value;
    }

    public int getValue() {
        return this.value;
    }

    public Treenode getLeftChild() {
        return this.left;
    }

    public Treenode getRightChild() {
        return this.right;
    }

    public void setLeftChild(final Treenode child) {
        this.left = child;
    }

    public void setRightChild(final Treenode child) {
        this.right = child;
    }

    @Override
    public int hashCode() {
        return this.value;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }

        if (getClass() != obj.getClass()) {
            return false;
        }

        final Treenode other = (Treenode) obj;

        return this.value == other.value;
    }

    @Override
    public String toString() {
        return "[" + this.value + "]";
    }
}


SearchTree.java:

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class SearchTree {

    private SearchTree() {}

    public static Treenode findLeastCommonAncestor(final Treenode root,
                                                   final Treenode node1,
                                                   final Treenode node2) {
        final List&lt;Treenode&gt; list1 = findPath(root, node1);

        if (list1 == null) {
            return null;
        }

        final List&lt;Treenode&gt; list2 = findPath(root, node2);

        if (list2 == null) {
            return null;
        }

        final Set&lt;Treenode&gt; visitedSet = new HashSet&lt;&gt;(list2);

        for (int i = list1.size() - 1; i &gt;= 0; --i) {
            final Treenode node = list1.get(i);

            if (visitedSet.contains(node)) {
                return node;
            }
        }

        return null;
    }

    private static List&lt;Treenode&gt; findPath(final Treenode root,
                                           final Treenode node) {
        final Map&lt;Treenode, Treenode&gt; parentMap = new HashMap&lt;&gt;();
        dfs(root, null, parentMap);

        if (!parentMap.containsKey(node)) {
            return null;
        }

        final List&lt;Treenode&gt; path = new ArrayList&lt;&gt;();
        Treenode current = node;

        while (current != null) {
            path.add(current);
            current = parentMap.get(current);
        }

        Collections.&lt;Treenode&gt;reverse(path);
        return path;
    }

    private static void dfs(final Treenode currentTreenode, 
                            final Treenode parentTreenode,
                            final Map&lt;Treenode, Treenode&gt; parents) {
        parents.put(currentTreenode, parentTreenode);

        if (currentTreenode.getLeftChild() != null) {
            dfs(currentTreenode.getLeftChild(), currentTreenode, parents);
        }

        if (currentTreenode.getRightChild() != null) {
            dfs(currentTreenode.getRightChild(), currentTreenode, parents);
        }
    }

    public static void main(String[] args) {
        /*
              n1                   5
             /  \                 / \
           n2    n3    ----&gt;     2   4
          /  \                  / \
         n4  n5                1   3

        */
        Treenode n1 = new Treenode(5);
        Treenode n2 = new Treenode(2);
        Treenode n3 = new Treenode(4);
        Treenode n4 = new Treenode(1);
        Treenode n5 = new Treenode(3);
        Treenode disconnected = new Treenode(6);

        n1.setLeftChild(n2);
        n1.setRightChild(n3);
        n2.setLeftChild(n4);
        n2.setRightChild(n5);

        System.out.println(findLeastCommonAncestor(n1, n4, n3));
        System.out.println(findLeastCommonAncestor(n1, n2, disconnected));
    }
}


Hope that helps.