binary trees print

  #include<iostream>


#include<vector>


#include<queue>


using namespace std;


template <typename T>



class binaryTree{

    public:

    T data;

    binaryTree * left;

    binaryTree * right;

    

    binaryTree(T data){

        this->data=data;

        left=NULL;

        right=NULL;

    }

    ~binaryTree(){

        delete left;

        delete right;

    }

};


void printTree(binaryTree<int>* root){

    if(root==NULL){

        return;

    }

    cout<<root->data<<":";

    if(root->left!=NULL){

        cout<<"L"<<root->left->data<<" ";

        

    }

    if(root->right!=NULL){

        cout<<"R"<<root->right->data<<" ";

        

    }

    cout<<endl;

    printTree(root->left);

    printTree(root->right);

}


int main(){

    binaryTree<int>* root = new binaryTree<int>(1);

    binaryTree<int>* node1 = new binaryTree<int>(2);

    binaryTree<int>* node2 = new binaryTree<int>(3);

    

    root->left=node1;

    root->right=node2;

    printTree(root);

}

Comments

Popular posts from this blog

Sum of Even Numbers till N

Find the Runner-Up Score!

Print All Substrings