当前位置:网站首页>108. Convert an ordered array into a binary search tree
108. Convert an ordered array into a binary search tree
2022-04-23 09:09:00 【yitahutu79】
Give you an array of integers nums , Where elements have been Ascending array , Please turn it into a Highly balanced Binary search tree .
Highly balanced A binary tree is a tree that satisfies 「 The absolute value of the height difference between the left and right subtrees of each node does not exceed 1 」 Two fork tree .
Example 1:
Input :nums = [-10,-3,0,5,9]
Output :[0,-3,9,-10,null,5]
explain :[0,-10,5,null,-3,null,9] Will also be considered the right answer :
Example 2:
Input :nums = [1,3]
Output :[3,1]
explain :[1,null,3] and [3,1] They're all highly balanced binary search trees .
Tips :
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums Press Strictly increasing Sequential arrangement
/** * 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;
int pos = numsSize / 2;
struct TreeNode *root = (struct TreeNode*)malloc(sizeof(struct TreeNode));
root->val = nums[pos];
root->left = sortedArrayToBST(nums, pos);
root->right = sortedArrayToBST(nums + pos + 1, numsSize - pos - 1);
return root;
}
版权声明
本文为[yitahutu79]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230904164798.html
边栏推荐
- How to render web pages
- PLC的点表(寄存器地址和点表定义)破解探测方案--方便工业互联网数据采集
- RSA 加密解密签名验签
- 爬虫使用xpath解析时返回为空,获取不到相应的元素的原因和解决办法
- 小女孩行走
- Wechat applet catchtap = "todetail" event problem
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- [original] use system Text. JSON formats the JSON string
- Go language self-study series | golang structure pointer
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
猜你喜欢
SAP 101K 411k inventory change
MySQL小练习(仅适合初学者,非初学者勿进)
Download and install bashdb
[C language] document operation
[in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
调包求得每个样本的k个邻居
MySQL小練習(僅適合初學者,非初學者勿進)
How to protect open source projects from supply chain attacks - Security Design (1)
Consensus Token:web3. 0 super entrance of ecological flow
The most concerned occupations after 00: civil servants ranked second. What was the first?
随机推荐
How does kubernetes use harbor to pull private images
Whether the same binary search tree (25 points)
Is Zhongyan futures safe and reliable?
How to protect open source projects from supply chain attacks - Security Design (1)
Colorui solves the problem of blocking content in bottom navigation
Non duplicate data values of two MySQL query tables
653. 两数之和 IV - 输入 BST
Judgment on heap (25 points) two insertion methods
npm ERR! network
Illegal character in scheme name at index 0:
Use include in databinding
Concave hull acquisition method based on convex hull of point cloud
Rembg split mask
Applet in wechat and app get current ()
Illegal character in scheme name at index 0:
L2-024 部落 (25 分)(并查集)
Failed to download esp32 program, prompting timeout
Wechat: get the owner of a single tag
L2-022 重排链表 (25 分)(map+结构体模拟)
Introduction to matlab