怎样编写一个程序,把一个有序整数数组放到二叉树中
思路:肯定不能偏向一侧,所以根节点应该为最中间的那个元素
之后再递归处理
Node * array2Tree(int[] array) {
return helper(array, 0, n-1);
}
Node * helper(int[] array, int start, int end) {
if (start > end) return NULL;
int m = start + (end-start)/2;
Node * root = new Node(array[m]);
root->left = helper(array, start, m-1);
root->right = helper(array, m+1, end);
return root;
}