当前位置:网站首页>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;
}
边栏推荐
- 混合app的介绍
- 机器学习笔记 - 时间序列作为特征
- 网络爬虫是什么
- What is digitalization? What is digital transformation? Why do enterprises choose digital transformation?
- [Yocto RM]9 - QA Error and Warning Messages
- LMSOC:一种对社会敏感的预训练方法
- Arrays. Aslist() pit
- Solon 1.8.3 发布,云原生微服务开发框架
- [DNS resolution] set the name DNSPod resolution for domain name access of COM
- 完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
猜你喜欢

Evaluation - grey correlation analysis

Neural network of zero basis multi map detailed map

药物发现综述-02-分子性质预测

自监督学习与药物发现

Review of drug discovery-03-molecular design and optimization

Original | 2025 to achieve the "five ones" goal! The four products of Jiefang power are officially released

MySQL 18: execution of write statements

The practice of dual process guard and keeping alive in IM instant messaging development

Data analysts too hot? Monthly income 3W? Tell you the true situation of this industry with data

数据库的新选择 Amazon Aurora
随机推荐
N methods of data De duplication using SQL
[open source] open source system sorting - Examination Questionnaire, etc
What is the application scope and function of the conductive slip ring of single crystal furnace
1382. 将二叉搜索树变平衡-常规方法
嵌入式必学,硬件资源接口详解——基于ARM AM335X开发板 (上)
Is it safe to open an account online now? Novice is just on the road, ask for the answer
Want to open an account to buy stock, is it safe to open an account on the Internet?
解决ionic4 使用hammerjs手势 press 事件,页面无法滚动问题
LMSOC:一种对社会敏感的预训练方法
Interviewer asked: Inheritance of JS
【MySQL】-【函数】
Hi, you have a code review strategy to check!
如何阅读一篇论文
Maimai hot post: Why are big factories keen on making wheels?
嵌入式必学!硬件资源接口详解——基于ARM AM335X开发板 (下)
Why stainless steel swivel
【DNS 解析】将Name.com的域名接入DNSPod解析
万字长文看懂商业智能(BI)|推荐收藏
PV操作原语
Interface component telerik UI for WPF Getting Started Guide - how to switch custom styles using themes