当前位置:网站首页>Leetcode 108. 将有序数组转换为二叉搜索树
Leetcode 108. 将有序数组转换为二叉搜索树
2022-08-11 03:29:00 【LuZhouShiLi】
Leetcode 108. 将有序数组转换为二叉搜索树
题目
给你一个整数数组 nums ,其中元素已经按 升序 排列,请你将其转换为一棵 高度平衡 二叉搜索树。高度平衡 二叉树是一棵满足「每个节点的左右两个子树的高度差的绝对值不超过 1 」的二叉树。
思路
- 每次取中间位置的元素构造节点,TreeNode* root = new TreeNode(nums[mid]);
- 接着划分区间,root的左孩子接住下一层左区间的构造节点,右孩子接住下一层右区间构造的节点
- 最后返回root节点
代码
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */
class Solution {
public:
TreeNode* traversal(vector<int>& nums,int left,int right)
{
if(left > right)
{
return nullptr;
}
int mid = left + ((right - left) / 2);
TreeNode* root = new TreeNode(nums[mid]);//左右区间的分割点
root->left = traversal(nums,left,mid -1);
root->right = traversal(nums,mid + 1,right);
return root;
}
TreeNode* sortedArrayToBST(vector<int>& nums) {
TreeNode* root = traversal(nums,0,nums.size() - 1);
return root;
}
};
边栏推荐
- Detailed explanation of VIT source code
- leetcode: 358. Reorder strings at K distance intervals
- 获取链表长度
- Watch to monitor
- How can users overcome emotional issues in programmatic trading?
- Traversal of DOM tree-----modify styles, select elements, create and delete nodes
- Docker 链接sqlserver时出现en-us is an invalid culture错误解决方案
- (CVPR - 2017) in depth and potential body learning context awareness feature for pedestrian recognition
- 大马驮2石粮食,中马驮1石粮食,两头小马驮一石粮食,要用100匹马,驮100石粮食,如何分配?
- Is there any way for kingbaseES to not read the system view under sys_catalog by default?
猜你喜欢
DNS separation resolution and intelligent resolution
DOM-DOM tree, a DOM tree has three types of nodes
C语言之自定义类型------结构体
添加用户报错useradd: cannot open /etc/passwd
Multi-merchant mall system function disassembly 26 lectures - platform-side distribution settings
没想到MySQL还会问这些...
Homework 8.10 TFTP protocol download function
Kubernetes集群搭建Zabbix监控平台
QueryDet:级联稀疏query加速高分辨率下的小目标检测
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
随机推荐
轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组
成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
A large horse carries 2 stone of grain, a middle horse carries 1 stone of grain, and two ponies carry one stone of grain. It takes 100 horses to carry 100 stone of grain. How to distribute it?
KingbaseES有什么办法,默认不读取sys_catalog下的系统视图?
Meaning of df and df -lh
电商项目——商城限时秒杀功能系统
IDE compilation error: Dangling metacharacter
AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis
常用认证机制
DNS分离解析和智能解析
【LeetCode】Day112-repetitive DNA sequence
The negative semantic transformation layer
JS-DOM element object
What has programmatic trading changed?
Redis老了吗?Redis与Dragonfly性能比较
What kind of programming trading strategy types can be divided into?
typedef定义结构体数组类型
App基本框架搭建丨日志管理 - KLog
[BX]和loop
LeetCode Hot Questions (12. The Best Time to Buy and Sell Stocks)