Print BST from array of strings where every string contains leaf nodes removed in iteration

Last Updated : 27 Sep, 2023

From a Binary Search Tree(BST), the leaf nodes are removed to get another tree, then leaf nodes are removed from that tree also. The process is continued till all the nodes of the tree are removed. Given an array of strings where string contains the leaf nodes removed in iteration. Print pre-order traversal of BST.


Input = {“BDHPY”,”CM”,”GQ”,”K”}

Input = {“K”}
Output = K
We can find preorder traversal by using two properties. 1.In BST all nodes left to root are smaller and right to root are greater then root 2.Preorder traversal is Node -> Left -> Right It is clear that last string in input must be root and first string must be leaves of original tree. By above mention property we can divide first string from input array into two parts. Leaves in left of root say leftTree and leaves in right of root say rightTree. Now for all other input strings we just need to append character either in front of leftTree or rightTree. Below is implementation in Java
  public class PreOrderFromLeaf {

    public static void findPreOrder(String[] input){
        //Edge case 1
        if(input == null || input.length < 1){
            System.out.println("Wrong Input");
        int size = input.length;
        char[] rootArray = input[size-1].toCharArray();
        //Edge case 2
            System.out.println("Wrong Input");
        //Edge case 3
        if(input.length == 1){
            System.out.println("PreOrder of Tree is :: " + input[0]);
        // Root element of main tree
        char root = rootArray[0];
        // First string in input must be leaf's of original tree.
        String leafs = input[0];
        //This will left and right subtree's leafs
        int index = getIndex(root,leafs);
        String leftTree =  leafs.substring(0, index);
        String rightTree = leafs.substring(index);
        for(int i =1; i<input.length-1;i++){
            String newleaf = input[i];
            for(int j =0;j<newleaf.length();j++){
                char newChar = newleaf.charAt(j);
                if(newChar <= root){
                    leftTree = newChar + leftTree;
                } else {
                    rightTree = newChar + rightTree;
        String preOrder = root + leftTree + rightTree;
        System.out.println("PreOrder of Tree is :: " + preOrder);

    //Helper function to divide left and right leaves
    public static int getIndex(char root, String leafArray){
        int index = leafArray.length();
        for(int i=0;i<leafArray.length();i++){
            if(leafArray.charAt(i) > root){
                index = i;
        return index;    

    public static void main(String args[]){
        String[] input = null;
        String[] input1 = {"K"};
        String[] input2 = {"A","B","C"};
        String[] input3 = {"BDHPY","CM","GQ","K"};
        String[] input4 = {"ACFH","BE","G","D"};


Wrong Input
PreOrder of Tree is :: K
PreOrder of Tree is :: CBA
PreOrder of Tree is :: KGCBDHQMPY
PreOrder of Tree is :: DBACGEFH

This article is compiled by Varun Jain. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

Share your thoughts in the comments

Similar Reads