title: How do i properly add characters to string in recursion
body: <p>So i have this function  EDIT:Whole program as requested</p>
  <pre><code>//This is a java program to construct Expression Tree using Infix Expression
  import java.io.*;
  public class Infix_Expression_Tree
  {
      public static void main(String args[]) throws IOException
      {
          
          String result=&quot;&quot;;
         
          File vhod = new File(args[0]);
          try{
              BufferedReader reader = new BufferedReader(new FileReader(vhod));
          
              Tree t1 = new Tree();
              
              String a = reader.readLine();
              t1.insert(a);
              t1.traverse(1,result);
              System.out.println(&quot;rez &quot;+ result);
             
              ch = reader.readLine();
      
      }catch(IOException e){
              e.printStackTrace();
          }
      }
  }
   
  class Node
  {
      public char data;
      public Node leftChild;
      public Node rightChild;
   
      public Node(char x)
      {
          data = x;
      }
   
      public void displayNode()
      {
          System.out.print(data);
      }
  }
   
  class Stack1
  {
      private Node[] a;
      private int    top, m;
   
      public Stack1(int max)
      {
          m = max;
          a = new Node[m];
          top = -1;
      }
   
      public void push(Node key)
      {
          a[++top] = key;
      }
   
      public Node pop()
      {
          return (a[top--]);
      }
   
      public boolean isEmpty()
      {
          return (top == -1);
      }
  }
   
  class Stack2
  {
      private char[] a;
      private int    top, m;
   
      public Stack2(int max)
      {
          m = max;
          a = new char[m];
          top = -1;
      }
   
      public void push(char key)
      {
          a[++top] = key;
      }
   
      public char pop()
      {
          return (a[top--]);
      }
   
      public boolean isEmpty()
      {
          return (top == -1);
      }
  }
   
  class Conversion
  {
      private Stack2 s;
      private String input;
      private String output = &quot;&quot;;
   
      public Conversion(String str)
      {
          input = str;
          s = new Stack2(str.length());
      }
   
      public String inToPost()
      {
          for (int i = 0; i &lt; input.length(); i++)
          {
              char ch = input.charAt(i);
              switch (ch)
              {
                  case '+':
                      gotOperator(ch, 2);
                      break;
                  case '*':
                      gotOperator(ch,1);
                      break;
                  case '/':
                      gotOperator(ch, 3);
                      break;
                  case '(':
                      s.push(ch);
                      break;
                  case ')':
                      gotParenthesis();
                      //s.pop();
                      break;
                  default:
                      //gotOperator(ch, 0);
                  //break;
                     output = output + ch;
              }
          }
          while (!s.isEmpty())
              output = output + s.pop();
  
          //System.out.println(&quot;to je output iz inToPost &quot; +output);
          return output;
      }
   
      private void gotOperator(char opThis, int prec1)
      {
          while (!s.isEmpty())
          {
              char opTop = s.pop();
              if (opTop == '(')
              {
                  s.push(opTop);
                  break;
              } else
              {
                  int prec2;
                  if (opTop == '+')
                      prec2 = 2;
                  else if(opTop=='*')
                      prec2=1;
                  else
                      prec2 = 3;
                  if (prec2 &lt;= prec1)
                  {
                      s.push(opTop);
                      break;
                  } else
                      output = output + opTop;
              }
          }
          s.push(opThis);
      }
   
      private void gotParenthesis()
      {
          while (!s.isEmpty())
          {
              char ch = s.pop();
              if (ch == '(')
                  break;
              else
                  output = output + ch;
          }
      }
  }
   
  class Tree
  {
      private Node root;
   
      public Tree()
      {
          root = null;
      }
   
      public void insert(String s)
      {
          Conversion c = new Conversion(s);
          s = c.inToPost();
          
          Stack1 stk = new Stack1(s.length());
          s = s + &quot;#&quot;;
          int i = 0;
          char symbol = s.charAt(i);
          Node newNode;
          while (symbol != '#')
          {
              if (symbol &gt;= '0' &amp;&amp; symbol &lt;= '9' || symbol &gt;= 'A'
                      &amp;&amp; symbol &lt;= 'Z' || symbol &gt;= 'a' &amp;&amp; symbol &lt;= 'z')
              {
                  newNode = new Node(symbol);
                  stk.push(newNode);
              } else if (symbol == '+' || symbol == '/'
                      || symbol == '*')
              {
                  Node ptr1=null;
                  Node ptr2=null;
                  //if(!stk.isEmpty()){
                  ptr1 = stk.pop();
                  if(!stk.isEmpty()){
                  ptr2 = stk.pop();
                  }
                  //}
                  newNode = new Node(symbol);
                  newNode.leftChild = ptr2;
                  newNode.rightChild = ptr1;
                  stk.push(newNode);
              }
              /*else if(symbol=='/'){
                  Node ptr = stk.pop();
                  newNode = new Node(symbol);
                  newNode.leftChild = ptr;
                  newNode.rightChild=null;
                  stk.push(newNode);
  
              }*/
              symbol = s.charAt(++i);
          }
          root = stk.pop();
      }
   
      public void traverse(int type,String result)
      {
          
                  System.out.println(&quot;Preorder Traversal:-    &quot;);
                  preOrder(root,result);
               
      }
   
      private void preOrder(Node localRoot, String result)
      {   
          if(root==null){
              return;
          }
          
          if (localRoot != null)
          {
              if(localRoot.data == '/'){
              preOrder(localRoot.leftChild,result);
              result=result + localRoot.data;
              //StringBuilder stringBuilder1 = new StringBuilder();
              //stringBuilder1.append(result).append(localRoot.data);
              System.out.println(result);
              //localRoot.displayNode();
              preOrder(localRoot.rightChild,result);
              return;
  
             
          }else{
               //System.out.println(&quot;trenutni root je&quot; );
               //localRoot.displayNode();
               result=result + localRoot.data;
              // StringBuilder stringBuilder1 = new StringBuilder();
              //stringBuilder1.append(result).append(localRoot.data);
              System.out.println(result);
               preOrder(localRoot.leftChild,result);
               //result=result + localRoot.data;
               //System.out.print(root.data);
               preOrder(localRoot.rightChild,result);
              
              //System.out.print(root.data);
              //preOrder(localRoot.rightChild);
              return;
              }
          }
      }
  }
  </code></pre>
  <p>my problem with it is that with localRoot.DisplayNode() i get the result i want. But when i add the same thing to the String result it adds data, until i get to leaf of the tree(leaf doesnt have left/right child) so it returns to previous recursive call, and goes to right child, here somewhere  the leaf(from which we came back) isnt in String anymore. How do i fix this?</p>
  <p>String is defined in main method</p>