Vertical order Print Binary Tree TLE

Getting TLE in one of the test cases.
Here is the code.

hi kishan
consider your code for the following test cases
1 1125212117 -1 -1 -1
expected output: 1125212117
your output: not producing anything

I have made the changes, replaced all ‘int’ by ‘long long int’. Along with that you need to pass number of levels in input. Here in the test case provided by you k = 2, and nodes are 1 1125212117 -1 -1 -1, so the output shown is
1125212117
1

still not able to rectify the error?

If i am not considering ‘n’ or ‘k’, how would i know when to stop taking the inputs. The recurrence for printing the vertical order is correct.

for that you need to change tour buildtree function
you can refer this
node* buildTreeLevelWise(){

int d;
cin>>d;

node*root = new node(d);
queue<node*> q;
q.push(root);

while(!q.empty()){

	node*f = q.front();
	q.pop();
	int c1,c2;
	cin>>c1>>c2;

	if(c1!=-1){
		f->left = new node(c1);
		q.push(f->left);
	}
	if(c2!=-1){
		f->right = new node(c2);
		q.push(f->right);
	}
}
return root;

}

1 Like

Thank you ma’am. But like how’d i get to know that there’s no use of input n. Like how was input n affecting the complexity. It was just under an if condition. I didn’t use any loop.