title: code for pruning a binary tree
body: <p>I am trying to write a code for removing the leaves of a tree and printing the rest of tree.</p>
  
  <p>I am able to get proper output in case of a balanced tree. for example</p>
  
  <pre><code>              1
               / \
              2   3
             / \    \
            4  5     6
  </code></pre>
  
  <p>The output of this tree must be 213 (when printed inorder)</p>
  
  <p>some how I am not getting desired output when it is a unbalanced tree. for ex</p>
  
  <pre><code>              1
               / \
              2   3
             / \    \
            4  5     6
           / \
          7   8
  </code></pre>
  
  <p>I am getting  <code>4 3 2 5 1 3. which is wrong.</code></p>
  
  <pre><code>correct answer is 4 2 1 3.(printed inorder)
  </code></pre>
  
  <p>Can somebody help me what mistake I am making?</p>
  
  <p>The code for the program is</p>
  
  <pre><code>    //prune a tree
  
  
      #include&lt;iostream&gt;
      using namespace std;
  
      struct Node{
          int data;
          struct Node *left;
          struct Node *right;
      };
  
      struct Node *follower=NULL;
      struct Node *create_node(int item)
      {
          struct Node *newNode=NULL;
          newNode= new Node;
          newNode-&gt;data=item;
          newNode-&gt;left=NULL;
          newNode-&gt;right=NULL;
  
          return newNode;
      }
  
      void extract_leaves_binary_tree(struct Node *root)
      {
          int na=0;
          if(root==NULL)
          return;
  
          if(root-&gt;left==NULL &amp;&amp; root-&gt;right==NULL)
          {
              na=1;
          }   
          else
          {
              follower=root;
          }
  
          if(na==1 )
          {       
              if(follower-&gt;left==root)
                  follower-&gt;left=NULL;            
              if(follower-&gt;right==root)
                  follower-&gt;right=NULL;                   
              na=0;
          }   
          extract_leaves_binary_tree(root-&gt;left);
          extract_leaves_binary_tree(root-&gt;right);
  
      }
  
  
      void print(struct Node *root)
      {
          if(root==NULL)
          return;
  
          print(root-&gt;left);
          cout&lt;&lt;root-&gt;data;
          print(root-&gt;right);
      }
  
      void driver(struct Node *root)
      {
  
          extract_leaves_binary_tree(root);
  
      }
  
      int main()
      {
          struct Node *root=NULL;
          root  = create_node(1);
           root-&gt;left = create_node(2);
           root-&gt;right = create_node(3);
           root-&gt;left-&gt;left = create_node(4);
           root-&gt;left-&gt;right = create_node(5);
           root-&gt;right-&gt;right = create_node(6);
           root-&gt;left-&gt;left-&gt;left = create_node(7);
          root-&gt;left-&gt;left-&gt;right = create_node(8);
  
          driver(root);
          cout&lt;&lt;"inordrer"&lt;&lt;endl;
          print(root);
          return 0;
      }
  </code></pre>