Wrong Input format in the Question

Hello Sir/Madam
I think the question is wrong. I mean how do I take input, like it is difficult to know when a boolean value is going to come or when an integer value will be coming.

Hey @hargovind
Input has to be taken in the following way:-
Consider the following example-
10 true 20 true 40 false false true 50 false false true 30 true 60 false false true 73 false false

Basically, true means that you have a child, and false means no child, and thus for input , you will take data and along with that you need to take a string value, so firstly you will take data value, then string is true, means that 20 is the left child, then again true means that 40 is the left child of 20, and then you have false which means that there is no left child , and then again false means that you dont have a right child , you have to follow this approach for building tree

Hey @mahimahans111 Thank you
I’ll try again, but if I didn’t get it, I’ll ask again relating to a basic idea of the code on building the tree!
Thanks

1 Like

@hargovind
okay great

I am unable to get an idea like when to stop taking the input, I can’t think of any approach!

Hey @mahimahans111
I’ve spent enough time thinking about the approaches to implement buildTree() function. Please help me with some code so that I can get an idea and implement it.
Thank You

Hey @hargovind

node * buildtree()
{
int cdata;
cin>>cdata;
node * root= new node(cdata);

		// left
		string hlc;
        cin>>hlc;

		if (hlc=="true") {
			root->left = buildtree();
		}

		// right
        string hrc;
        cin>>hrc;

		if (hrc=="true") {
			root->right = buildtree();
		}

		// return
		return root;

}

1 Like

Hey @mahimahans111
Thanks a lot it worked, I was also doing with similar approach but was making some mistake, will compare with your code, Good to go!
Thanks!

@hargovind that’s great !!

1 Like