Print all possible sequences that could have created this BST
public class Solution {
    ArrayList&lt;LinkedList&lt;Long&gt;&gt; result;
    /*Return the children of a node */
    ArrayList&lt;TreeNode&gt; getChilden(TreeNode parent) {
        ArrayList&lt;TreeNode&gt; child = new ArrayList&lt;TreeNode&gt;();
        if(parent.left != null) child.add(parent.left);
        if(parent.right != null) child.add(parent.right);
        return child;
    }
    /*Gets all the possible Compinations*/
    void getPermutations(ArrayList&lt;TreeNode&gt; permutations, LinkedList&lt;Long&gt; current) {
        if(permutations.size() == 0) {
            result.add(current);
            return;
        }
        int length = permutations.size();
        for(int i = 0; i &lt; length; i++) {
            TreeNode node = permutations.get(i);
            permutations.remove(i);
            ArrayList&lt;TreeNode&gt; newPossibilities = new ArrayList&lt;TreeNode&gt;();
            newPossibilities.addAll(permutations);
            newPossibilities.addAll(getChilden(node));
            LinkedList&lt;Long&gt; newCur = new LinkedList&lt;Long&gt;();
            newCur.addAll(current);
            newCur.add(node.val);
            getPermutations(newPossibilities, newCur);
            permutations.add(i,node);
        }
    }

    /*This method returns a array of arrays which will lead to a given BST*/
    ArrayList&lt;LinkedList&lt;Long&gt;&gt; inputSequencesForBst(TreeNode node) { 
        result = new ArrayList&lt;LinkedList&lt;Long&gt;&gt;();
        if(node == null)
            return result;
        ArrayList&lt;TreeNode&gt; permutations = getChilden(node);
        LinkedList&lt;Long&gt; current = new LinkedList&lt;Long&gt;();
        current.add(node.val);
        getPermutations(permutations, current);
        return result;
    }
}


My solution. Works perfectly.