首页 > 学院 > 开发设计 > 正文

108. Convert Sorted Array to Binary Search Tree

2019-11-10 17:48:56
字体:
来源:转载
供稿:网友

Q

https://leetcode.com/PRoblems/convert-sorted-array-to-binary-search-tree/

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

A

/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */struct TreeNode* sortedArrayToBST(int* nums, int numsSize) { if (numsSize == 0) { return NULL; } struct TreeNode *root; int mid; root = (struct TreeNode *)malloc(sizeof(struct TreeNode)); mid = numsSize/2; root->val = nums[mid]; root->left = sortedArrayToBST(nums, mid); root->right = sortedArrayToBST(nums+mid+1, numsSize-mid-1); return root;}
上一篇:poj2140

下一篇:poj1575

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表