From 80c6e5ddeb57634c54b930a484d73e47a2f87864 Mon Sep 17 00:00:00 2001 From: ektaa1 Date: Sat, 30 Oct 2021 09:48:38 +0530 Subject: [PATCH] Binary Tree formation in c++ --- Binary Tree Formation.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Binary Tree Formation.cpp diff --git a/Binary Tree Formation.cpp b/Binary Tree Formation.cpp new file mode 100644 index 0000000..da50606 --- /dev/null +++ b/Binary Tree Formation.cpp @@ -0,0 +1,39 @@ +#include +using namespace std; +class Node{ + public: + int data; + Node*left; + Node*right; + Node(int d){ + data=d; + left=NULL; + right=NULL; + } +}; +Node* buildtree(){ + int d; + cin>>d; + Node*root; + if(d==-1){ + return NULL; + } + root=new Node(d); + root->left=buildtree(); + root->right=buildtree(); + return root; +} +void print(Node*root){ + if(root==NULL){ + return; + } + cout<data<<" "; + print(root->left); + print(root->right); +} +int main() +{ + Node*root=buildtree(); + print(root); + return 0; +} \ No newline at end of file