当前位置:网站首页>108. 将有序数组转换为二叉搜索树
108. 将有序数组转换为二叉搜索树
2022-04-23 09:04:00 【yitahutu79】
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。
高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
示例 1:

输入:nums = [-10,-3,0,5,9]
输出:[0,-3,9,-10,null,5]
解释:[0,-10,5,null,-3,null,9] 也将被视为正确答案:

示例 2:
输入:nums = [1,3]
输出:[3,1]
解释:[1,null,3] 和 [3,1] 都是高度平衡二叉搜索树。

提示:
1 <= nums.length <= 104
-104 <= nums[i] <= 104
nums 按 严格递增 顺序排列
/** * 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://blog.csdn.net/qq_40713201/article/details/124357406
边栏推荐
- First principle mind map
- LLVM之父Chris Lattner:编译器的黄金时代
- Bk3633 specification
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- Multi view depth estimation by fusing single view depth probability with multi view geometry
- npm ERR! network
- On array replication
- Flink同时读取mysql与pgsql程序会卡住且没有日志
- [C language] document operation
- [original] use system Text. JSON formats the JSON string
猜你喜欢

2021 Li Hongyi's adaptive learning rate of machine learning

PLC point table (register address and point table definition) cracking detection scheme -- convenient for industrial Internet data acquisition

The most concerned occupations after 00: civil servants ranked second. What was the first?

Mini - exercice MySQL (seulement pour les débutants, pas pour les non - débutants)

Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)

LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands

SAP 101K 411K 库存变化

L2-022 重排链表 (25 分)(map+结构体模拟)

Download and install bashdb

Study notes of deep learning (8)
随机推荐
Play with binary tree (25 points)
【原创】使用System.Text.Json对Json字符串进行格式化
Pctp test experience sharing
K210 learning notes (II) serial communication between k210 and stm32
Single chip microcomputer nixie tube stopwatch
Restore binary tree (25 points)
Write down the post order traversal of the ~ binary tree
What is augmented reality technology? Where can it be used?
微信:获取单个标签所有人
Wechat: get the owner of a single tag
DJ music management software pioneer DJ rekordbox
Flink SQL realizes the integration of stream and batch
Notes d'apprentissage oneflow: de functor à opexprinterpreter
Group Backpack
Arbre de dépendance de l'emballage des ressources
How does kubernetes use harbor to pull private images
Download and install bashdb
Go language self-study series | golang structure as function parameter
Basic usage of synchronized locks
Project upload part