Double Tree with examples in C++ Program

In this tutorial, we are going to learn how to double the given tree.

Let's see the steps to solve the problem.

  • Create node class.

  • Initialize the tree with dummy data.

  • Write a recursive function to double the tree.

    • Recursively traverse through the tree.

    • Store the left node in a variable.

    • After traversing add the data by creating a new node.

    • Now, add the left node to the newly created node as a left child.

  • Print the tree.


Let's see the code.

 Live Demo

#include <bits/stdc++.h>
using namespace std;
class node {
   int data;
   node* left;
   node* right;
node* newNode(int data) {
   node* Node = new node();
   Node->data = data;
   Node->left = NULL;
   Node->right = NULL;
   return Node;
void doubleTree(node* Node) {
   node* oldLeft;
   if (Node == NULL) return;
   oldLeft = Node->left;
   Node->left = newNode(Node->data);
   Node->left->left = oldLeft;
void printTree(node* node) {
   if (node == NULL) {
   cout << node->data << " ";
int main() {
   node *root = newNode(1);
   root->left = newNode(2);
   root->right = newNode(3);
   cout << "Original Tree" << endl;
   cout << endl;
   cout << "Double Tree" << endl;
   cout << endl;
   return 0;


If you run the above code, then you will get the following result.

Original Tree
2 1 3
Double Tree
2 2 1 1 3 3


If you have any queries in the tutorial, mention them in the comment section.

Updated on: 28-Jan-2021


Kickstart Your Career

Get certified by completing the course

Get Started