当前位置:网站首页>1382. balancing binary search tree - General method
1382. balancing binary search tree - General method
2022-06-28 01:49:00 【Mr Gao】
1382. Balance the binary search tree
Here's a binary search tree , Please return to a tree After balancing Binary search tree of , The newly generated tree should have the same node value as the original tree . If there are multiple construction methods , Please return to any one .
If a binary search tree , The height difference between the two subtrees of each node shall not exceed 1 , We call this binary search tree The balance of .
Example 1:
Input :root = [1,null,2,null,3,null,4,null,null]
Output :[2,1,3,null,null,null,4]
explain : This is not the only correct answer ,[3,1,4,null,2,null,null] It is also a feasible construction scheme .
Example 2:
Input : root = [2,1,3]
Output : [2,1,3]
The solution code is as follows :
/** * 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;
}
边栏推荐
- What problems should be evaluated before implementing MES management system
- 【DNS 解析】将Name.com的域名接入DNSPod解析
- Lmsoc: a socially sensitive pre training method
- 面试官问:能否模拟实现JS的new操作符
- PostgreSQL setting auto increment field
- LMSOC:一种对社会敏感的预训练方法
- 205. 同构字符串
- TIA botu_ Concrete method of making analog input and output Global Library Based on SCL language
- Web3 技术初体验以及相关学习资料
- Interview guide for data person | prepare these points to be prepared!
猜你喜欢

Drug interaction prediction based on learning size adaptive molecular substructure

联想拯救者R720如何组建双通道内存

Database query optimization: master-slave read-write separation and common problems

1382. 将二叉搜索树变平衡-常规方法

【MySQL】-【函数】

Self supervised learning and drug discovery

向excel中导入mysql中的数据表

Web3 technology initial experience and related learning materials

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

【嵌入式基础】串口通信
随机推荐
Modular development
ionic4实现半星评分
国外LEAD赚钱的一些习惯
Drug interaction prediction based on learning size adaptive molecular substructure
Deepmind | pre training of molecular property prediction through noise removal
Proe/creo product structure design - continuous research
lefse分析本地实现方法带全部安装文件和所有细节,保证成功。
Is there any risk in opening an account for flush stock? Is it safe for flush to open an account
一张图弄懂 MIT,BSD,Apache几种开源协议之间的区别
Adobe Premiere Basics - common video effects (corner positioning, mosaic, blur, sharpen, handwriting tools, effect control hierarchy) (16)
What are the requirements for customizing the slip ring for UAV
Réseau neuronal pour la solution détaillée Multi - diagrammes de fondation zéro
Some habits of making money in foreign lead
什麼是數字化?什麼是數字化轉型?為什麼企業選擇數字化轉型?
Lefse analyzes the local implementation method with all installation files and details to ensure success.
Adobe Premiere基础-编辑素材文件常规操作(脱机文件,替换素材,素材标签和编组,素材启用,便捷调节不透明度,项目打包)(十七)
Evaluation - grey correlation analysis
Interview guide for data person | prepare these points to be prepared!
I/O限制进程与CPU限制进程
The contents of the latex table are left, middle and right