Question: I came across some threads on StackOverflow but none of them quite cleared my doubts.

So the problem is simple. I need to iteratively insert elements into a binary tree. And this is my code.

```
BST newNode(int x)
{
    BSTNodePtr node = (BSTNodePtr) malloc(sizeof(struct TreeNode));
    node->Element = x;
    node->Left = NULL;
    node->Right = NULL;

    return node;

}

BST Insert(int x, BST T)
{
    BST temp_node = T;
    while( T != NULL)   {
        if (x < T->Element)
            T = T->Left;
        else if (x >= T->Element)
            T = T->Right;
    }

    T = newNode(x);

    return temp_node;

}

```

However, when I'm finding the height of this tree I am always getting 0. The height code is

```
int Height(BST T)
{
    if (T == NULL)
        return 0;

    return 1+(max(Height(T->Left), Height(T->Right)));
}

```

and this works perfectly fine when I do insertion recursively (using a function with the exact same signature)

What am I missing?

Answer: 
Couldn't solve the problem that way. This code, however, seems to work.

```
BST Insert(int x, BST T)
    {
       BST temp=T;
       BST node=(BST)malloc(sizeof(struct TreeNode));
       node->Element=x;
       node->Left=NULL;
       node->Right=NULL;
       if (T==NULL)
       {
           T=node;
           return(T);
           //printf("%d\n",T->Element);
       }
       else
       {
           while(1)
           {
               if (temp->Element>=node->Element && temp->Left==NULL)
               {
                   temp->Left=node;
                   break;
               }
               else if (temp->Element>=node->Element && temp->Left!=NULL)
               {

                   temp=temp->Left;
               }
               else if (temp->Element<node->Element && temp->Right==NULL)
               {
                   temp->Right=node;
                   break;
               }
               else
               {
                   temp=temp->Right;
               }
           }   
                 return(T);
       }            
    }

```
Here:

```
BST Insert(int x, BST T)
{
    BST temp_node = T;
    while( T != NULL)   {
        if (x < T->Element)
            T = T->Left;
        else if (x >= T->Element)
            T = T->Right;
    }

    T = newNode(x);

    return temp_node;

}

```

You navigate the tree until you hit `T == NULL`. Then you create a node and assign the pointer to it to `T`. Then you return the original value of `T`. You don't modify your tree at all. No node in it is made to point to the newly created node. `T` is just a local variable.