当前位置:网站首页>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;
}
};
边栏推荐
- Rotary array problem: how to realize the array "overall reverse, internal orderly"?"Three-step conversion method" wonderful array
- C语言之自定义类型------结构体
- pathman_config、pathman_config_params 删除后,如何重建?
- Salesforce disbands the Chinese team, which CRM product is more suitable for the Chinese
- Redis老了吗?Redis与Dragonfly性能比较
- Audio codec, using FAAC to implement AAC encoding
- Goodbye Chengdu paper invoices!The issuance of electronic invoices for accommodation expenses will soon completely replace the invoices of hotels, catering and gas stations
- The problem that Merge will be lost again after code Revert has been solved
- 多商户商城系统功能拆解26讲-平台端分销设置
- DNS分离解析和智能解析
猜你喜欢

Idea (优选)cherry-pick操作

VIT 源码详解

电力机柜数据监测RTU

Detailed explanation of VIT source code

font

AI+Medical: Using Neural Networks for Medical Image Recognition and Analysis

电商项目——商城限时秒杀功能系统

轮转数组问题:如何实现数组“整体逆序,内部有序”?“三步转换法”妙转数组

Traversal of DOM tree-----modify styles, select elements, create and delete nodes

What should I do if the channel ServerID is incorrect when EasyCVR is connected to a Hikvision Dahua device and selects another cluster server?
随机推荐
Meaning of df and df -lh
21天学习挑战赛第一周总结
21 Day Learning Challenge Week 1 Summary
2022-08-10 第六小组 瞒春 学习笔记
成都纸质发票再见!开住宿费电子发票即将全面取代酒店餐饮加油站发票
The thirteenth day of learning programming
QueryDet: Cascading Sparse Query Accelerates Small Object Detection at High Resolution
学编程的第十三天
Official release丨VS Code 1.70
Goodbye Guangzhou paper invoices!The issuance of electronic invoices for accommodation fees will completely replace the invoices of hotels, restaurants and gas stations
[BX]和loop
Unity2D animation (1) introduction to Unity scheme - animation system composition and the function of use
IDE compilation error: Dangling metacharacter
CTO说MySQL单表行数不要超过2000w,为啥?
云平台下ESB产品开发步骤说明
Build Zabbix Kubernetes cluster monitoring platform
EasyCVR接入海康大华设备选择其它集群服务器时,通道ServerID错误该如何解决?
When EasyCVR is connected to the GB28181 device, what is the reason that the device is connected normally but the video cannot be played?
构建程序化交易系统需要注意什么问题?
Paper Accuracy - 2017 CVPR "High-Resolution Image Inpainting using Multi-Scale Neural Patch Synthesis"