title: How to write the code for 'addToRightMostChildAsLeftChild(Node a,Node b)'?
body: <p>Lets say the above method has two arguments <code>a and b</code>:</p>
  
  <ul>
  <li><strong>a</strong> : represents the root node  of the tree for which we need to traverse till we get the right node </li>
  <li><strong>b</strong> : represents the node to be added to a's right most node as a left child.</li>
  </ul>
  
  <p>I just need to know how to add a `node to the <em>right most node</em> of the tree but as a <em>left child</em>. I'm actually solving a different version of <a href="https://www.geeksforgeeks.org/modify-binary-tree-get-preorder-traversal-using-right-pointers/#comment-4419662162" rel="nofollow noreferrer">this</a> problem. Here the problem is using <em>right pointers</em>.</p>
  
  <p>Construct a tree in such a way that traversing it only through left pointers generates <strong>pre-order traversal</strong> of the tree.</p>
  
  <p>Actually it can be solved by traversing the tree and maintaining a <code>previous node and linking them in the way we want :</code>prev.left = current`.</p>
  
  <p><strong>My way of approaching this problem was:</strong>
  If there's a tree as following</p>
  
  <p>(just add the node 2 to 5 as a left child
  and then 5 to 3 as left child and finally 6 to 4 as left child.)</p>
  
  <pre><code>                 10
                   / \
                  8   2
                 / \ / \
                3  5 4  6
  
  
                 10
                 /
                8
               / \
              3   5
                 /
                2
               / \
              4   6
  
               10
               /
              8
             / 
            3
           /
          5
         /
        2 
       / \
      4   6
               10
               /
              8
             / 
            3
           /
          5
         /
        2
       /
      4
     /
    6
  </code></pre>
  
  <p>10 8 3 5 2 4 6 which is the <code>pre-order traversal</code> of the tree</p>
  
  <p>I know it can be done by using `prev pointer and doing stuff. I want it do be done this way.</p>
  
  <pre><code>                 10        
                   / \
                  8   2      
                 / \ / \
                3  5 4  6
  
                   ||
                   \/
  
                  10
                 /
                8
               / 
              3
             /
            5
           /
          2
         /
        4
       /
      6
  </code></pre>
  
  <p><strong>Node is defined as:</strong></p>
  
  <pre><code> class Node{
      int data;
      Node left,right;
      Node(int d)
      {
          data=d;
          left=null;
          right=null;
      }
  }
  </code></pre>