Convert an array to a binary tree I wrote this code in C++ to convert an array to a binary tree.

For instance, int nums[] = {7,5,9,1,7,3} to a binary tree (root-7, root's left child-5, root's right child-9...).

( here, I made a function named 'create_tree()' which only take one argument )

Is there any shorter way to do this?

#include &lt;iostream&gt;
#include &lt;cstdlib&gt;
#include &lt;cstdio&gt;

template&lt;class E&gt;
struct TNode{
    E value;
    TNode&lt;E&gt;* leftChild;
    TNode&lt;E&gt;* rightChild;

    TNode&lt;E&gt;(){
        this-&gt;value = NULL;
        this-&gt;leftChild = NULL;
        this-&gt;rightChild = NULL;
    }

    TNode&lt;E&gt;(E value){
        this-&gt;value = value;
        this-&gt;leftChild = NULL;
        this-&gt;rightChild = NULL;
    }
};

template&lt;class E&gt;
class BTree{
private:
    TNode&lt;E&gt;* root;

    /**
        build and return the pointer to a binary tree for given array 'arr' and array         size 'len'
    */
    TNode&lt;E&gt;* build(E* arr, int len){

        TNode&lt;E&gt; *subRoot = new TNode&lt;E&gt;(arr[0]);
        if (len == 1){
            return subRoot;  // terminating function - at the leaf
        }
        E* elementsToLeft = new E[len];  // array to store the left subtree children
        E* elementsToRight = new E[len];  // array to store the right sub tree     children

        int counterLeft = 0;  
        int counterRight = 0;

        for (int i = 1; i &lt; len; i = (i * 2) + 1){
            for (int j = i; j &lt; i + ((i + 1) / 2); j++){
                elementsToLeft[counterLeft] = arr[j]; // appending the array which     has children of left sub tree
                counterLeft++;
                if ((j + (i + 1) / 2)&lt;len){  // check whether there are childs to     store in the right sub tree....
                    elementsToRight[counterRight] = arr[j + (i + 1) / 2]; //   appending the array which has children of left sub tree
                    counterRight++;
                }

            }
        }
        subRoot-&gt;leftChild = build(elementsToLeft, counterLeft);  // recursive call&gt;     array- left sub tree's children and size
        subRoot-&gt;rightChild = build(elementsToRight, counterRight); // recursive     call&gt; array- right sub tree's children and size
        return subRoot;
    }

public:

    /**
    add the value specified by newvalueas the left child of the tree node specified     by parent,if the node does not have a left childalready.
    returns 1 if addition is successful and returns -1 in failure.
    */
    template &lt;class E&gt;
    int intaddLeftChild(TNode&lt;E&gt;* parent, E newvalue){
        if (parent-&gt;leftChild != NULL){ return -1; }
        TNode&lt;E&gt; *temp = new TNode&lt;E&gt;(newvalue);
        parent-&gt;leftChild = temp;
        return 1;
    }

    /**add the value specified by newvalueas the right child of the tree node     specified by parent, if the node does not have a right childalready.
    returns 1 if addition is successful and returns -1 in failure.
    */
    template &lt;class E&gt;
    int intaddRightChild(TNode&lt;E&gt;* parent, E newvalue){
        if (parent-&gt;rightChild != NULL){ return -1; }
        TNode&lt;E&gt; *temp = new TNode&lt;E&gt;(newvalue);
        parent-&gt;rightChild = temp;
        return 1;
    }

    /**
    function to create and return the pointer to a Binary tree from an array
    input an array. eg:- int i[] = {3,5,7} ; create_tree(i);
    */
    template&lt;class E, int N&gt;
    TNode&lt;E&gt;* create_tree(E(&amp;elements)[N]){
        int length = N;
        return build(elements, N);
    }
};

int main(){
    //to demonstrate
    int nums[] = { 7,5,9,1,7,3};
    BTree&lt;int&gt; mal;

    TNode&lt;int&gt;* BinaryTree = mal.create_tree(nums); // store the made binary tree     from nums and point it by the pointer, BinaryTree.


}
Main points to improve:


Memory leaks: C++ is not garbage collected. Every new must be deleted at some point. This is one of the reasons why C++ has destructors, so that we can perform this and other kinds of cleanup. In modern C++, smart pointers and standard containers are preferable, over manual memory management, since forgetting to delete a pointer is a very easy to make mistake, thus manual memory management being a major source of bugs and memory leaks. 
Unusual data initialization style in constructors:


TNode&lt;E&gt;(){
    this-&gt;value = NULL;
    this-&gt;leftChild = NULL;
    this-&gt;rightChild = NULL;
}



The following is the correct way to initialize members. You should use the constructor initialization list. Also, prefixing the variables with a this-&gt; in something we rarely do in C++:

TNode()
    : value(NULL)
    , leftChild(NULL)
    , rightChild(NULL)
{ }



Other tidbits:


Inside the body of a template class, such as in TNode, you should omit the template parameter in the name. Writing TNode&lt;E&gt; everywhere is too verbose and unnecessary.
Template parameter name for types is by convention called T instead of E.
NULL is deprecated since C++11 in favor of the better nullptr.
Integer return codes are quite error prone. Returning a bool true or false, when the function has only two possible outcomes, is a lot nicer. 
You didn't follow your camelCase notation in the create_tree() method.