Use of static not clear

Why sir has used static in the following code - I was using
node* n = new node(pre[s]); - ( start index of array)
but it was not running - what is the need to initilize k as static?

Question - Construct Binary tree from Inorder and preOrder

hey @LPLC0114, because static variable can be declared only once, we need to traverse complete preorder array and make each of its element as node and find its position in inorder array to decide LST and RST of that node.

Now with i as static will help to iterate complete preorder array.

Okk cool Instead of static , can I use pre[s] to extract first element of preorder array.

hey @LPLC0114, no that s is to iterate over inorder array and indicates starting point of subtree.

Ok so it means to indicate index of preorder array, I will have to consider separate variable. I cannot use ā€˜s’ to refer root element of the preorder array

hey @LPLC0114, yes, you cannot use ā€˜s’ to refer root element of the preorder array

1 Like

Okk Now i got it. It means I need to keep a separate variable for indexing preorder array not the same as inorder array

yes @LPLC0114 , because preorder is used to decide root and inorder to decide subtrees of that root.