题解 | #将升序数组转化为平衡二叉搜索树#
将升序数组转化为平衡二叉搜索树
http://www.nowcoder.com/practice/7e5b00f94b254da599a9472fe5ab283d
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; * * C语言声明定义全局变量请加上static,防止重复定义 */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num int整型一维数组 * @param numLen int num数组长度 * @return TreeNode类 */ typedef struct TreeNode AVLNode; AVLNode* create(int** num,int left,int right){ if(left>right)return NULL; int mid=(left+right)/2; AVLNode* x=(AVLNode*)malloc(sizeof(AVLNode)); x->val=(*num)[mid]; x->left=create(num,left,mid-1); x->right=create(num,mid+1,right); return x; } struct TreeNode* sortedArrayToBST(int* num, int numLen ) { // write code here AVLNode* root=create(&num,0,numLen-1); return root; }