当前位置:网站首页>1382. 将二叉搜索树变平衡-常规方法
1382. 将二叉搜索树变平衡-常规方法
2022-06-27 23:12:00 【Mr Gao】
1382. 将二叉搜索树变平衡
给你一棵二叉搜索树,请你返回一棵 平衡后 的二叉搜索树,新生成的树应该与原来的树有着相同的节点值。如果有多种构造方法,请你返回任意一种。
如果一棵二叉搜索树中,每个节点的两棵子树高度差不超过 1 ,我们就称这棵二叉搜索树是 平衡的 。
示例 1:
输入:root = [1,null,2,null,3,null,4,null,null]
输出:[2,1,3,null,null,null,4]
解释:这不是唯一的正确答案,[3,1,4,null,2,null,null] 也是一个可行的构造方案。
示例 2:
输入: root = [2,1,3]
输出: [2,1,3]
解题代码如下:
/** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */
int len;
void or_search(struct TreeNode* root){
if(root){
or_search(root->left);
len++;
or_search(root->right);
}
}
void or_search2(struct TreeNode* root,int *a){
if(root){
or_search2(root->left,a);
a[len++]=root->val;
or_search2(root->right,a);
}
}
void creat_root(struct TreeNode** root,int *a,int low,int high){
if(low<=high){
int mid=(low+high)/2;
(*root)->val=a[mid];
(*root)->left=(struct TreeNode*)malloc(sizeof(struct TreeNode));
(*root)->right=(struct TreeNode*)malloc(sizeof(struct TreeNode));
creat_root(&((*root)->left),a,low,mid-1);
creat_root(&((*root)->right),a,mid+1,high);
}
else{
(*root)=NULL;
}
}
struct TreeNode* balanceBST(struct TreeNode* root){
len=0;
or_search(root);
struct TreeNode* re=(struct TreeNode*)malloc(sizeof(struct TreeNode));
int *a=(int *)malloc(sizeof(int)*len);
len=0;
or_search2(root,a);
printf("len %d ",len);
creat_root(&re,a,0,len-1);
return re;
}
边栏推荐
- 手机股票开户安全吗,买股票在哪开户?
- 同花顺上能炒股开户吗?安全吗?
- 【MySQL】-【函数】
- How to build an e-commerce platform at low cost
- centos8-操作记录-命令版-yum-redis-mysql-nacos-jdk
- What is the e-commerce conversion rate so abstract?
- 去哪儿网(Qunar) DevOps 实践分享
- 无人机专用滑环定制要求是什么
- Proe/creo product structure design - continuous research
- Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
猜你喜欢

评价——灰色关联分析

Form forms and form elements (input, select, textarea, etc.)

如何理解 Transformer 中的 Query、Key 与 Value

基于可学习尺寸自适应分子亚结构的药物相互作用预测

Adobe Premiere基础-声音调整(音量矫正,降噪,电话音,音高换挡器,参数均衡器)(十八)

Class文件结构和字节码指令集

Esxi based black Qunhui DSM 7.0.1 installation of VMware Tools

Web3 technology initial experience and related learning materials

零基础多图详解图神经网络

【牛客讨论区】第四章:Redis
随机推荐
Is it safe to open an account for mobile stocks? Where can I open an account for buying stocks?
MySQL - function
electron窗口背景透明无边框(可用于启动页面)
Why stainless steel swivel
Web mouse click special effects case collection (red heart in live broadcast room)
Collection de cas d'effets spéciaux en cliquant sur la souris de la page Web
Is there any risk in opening an account for flush stock? Is it safe for flush to open an account
Supervised, unsupervised and semi supervised learning
【嵌入式基础】内存(Cache,RAM,ROM,Flash)
Flutter SliverAppBar全解析,你要的效果都在这了!
Interface component telerik UI for WPF Getting Started Guide - how to switch custom styles using themes
[untitled]
Is it safe for Xiaobai in the stock market to open an account on the Internet?
Golang monkeys eat peaches and ask for the number of peaches on the first day
【MySQL】-【函数】
How to read a paper
Qu'est - ce que la numérisation? Qu'est - ce que la transformation numérique? Pourquoi les entreprises choisissent - elles la transformation numérique?
界面组件Telerik UI for WPF入门指南 - 如何使用主题切换自定义样式
【DNS 解析】将Name.com的域名接入DNSPod解析
Huawei partners and Developers Conference 2022 | Kirin software cooperates with Huawei to jointly build the computing industry and create a digital intelligence future