Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
首先我是要AVL树的创建过程进行操作,不过提交之后出现超时,但还是让我将AVL树的创建过程实现了一遍,记录一下:
C++实现代码:
#include<iostream> #include<new> #include<vector> #include<cmath> using namespace std;//Definition for binary tree struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution { public:TreeNode *sortedArrayToBST(vector<int> &num){if(num.empty())return NULL;TreeNode *root=NULL;int i;for(i=0; i<(int)num.size(); i++)insert(root,num[i]);return root;}void insert(TreeNode *&root,int key){int lhigh,rhigh;if(root==NULL){root=new TreeNode(key);if(root==NULL)return;}else if(key<root->val){insert(root->left,key);lhigh=maxDepth(root->left);rhigh=maxDepth(root->right);if(abs(lhigh-rhigh)>1){if(key<root->left->val)SingleRotateWithLeft(root);elseDoubleRotateWithLeft(root);}}else{insert(root->right,key);lhigh=maxDepth(root->left);rhigh=maxDepth(root->right);if(abs(lhigh-rhigh)>1){if(key>root->right->val)SingleRotateWithRight(root);elseDoubleRotateWithLeft(root);}}}int maxDepth(TreeNode *root){if(root){if(root->left==NULL&&root->right==NULL)return 1;int leftDepth=maxDepth(root->left);int rightDepth=maxDepth(root->right);return leftDepth>= rightDepth ?(leftDepth+1):(rightDepth+1);}return 0;}void SingleRotateWithLeft(TreeNode *&root){TreeNode *l=root->left;root->left=l->right;l->right=root;root=l;}void SingleRotateWithRight(TreeNode *&root){TreeNode *r=root->right;root->right=r->left;r->left=root;root=r;}void DoubleRotateWithLeft(TreeNode *&root){SingleRotateWithRight(root->left);SingleRotateWithLeft(root);}void DoubleRotateWithRight(TreeNode *&root){SingleRotateWithLeft(root->right);SingleRotateWithRight(root);}void inorder(TreeNode *root){if(root){inorder(root->left);cout<<root->val<<" ";inorder(root->right);}} };int main() {Solution s;TreeNode *root=NULL;vector<int> vec={1,2,3,4,5,6,7,8,9,10};root=s.sortedArrayToBST(vec);s.inorder(root);cout<<endl;cout<<s.maxDepth(root)<<endl; }
运行结果:
提交的代码:
#include<iostream> #include<new> #include<vector> #include<cmath> using namespace std;//Definition for binary tree struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {} };class Solution { public:TreeNode *sortedArrayToBST(vector<int> &num){if(num.empty())return NULL;//递归的退出条件,一定要写哦TreeNode *root=NULL;int left=0;int right=num.size()-1;int mid=(left+right)/2;root=new TreeNode(num[mid]);vector<int> l(mid);vector<int> r(right-mid);int i;for(i=0;i<mid;i++)l[i]=num[i];for(i=mid+1;i<(int)num.size();i++)r[i-mid-1]=num[i];root->left=sortedArrayToBST(l);root->right=sortedArrayToBST(r);return root;}int maxDepth(TreeNode *root){if(root){if(root->left==NULL&&root->right==NULL)return 1;int leftDepth=maxDepth(root->left);int rightDepth=maxDepth(root->right);return leftDepth>= rightDepth ?(leftDepth+1):(rightDepth+1);}return 0;}TreeNode* SingleRotateWithLeft(TreeNode *&root){TreeNode *l=root->left;root->left=l->right;l->right=root;return l;}TreeNode* SingleRotateWithRight(TreeNode *&root){TreeNode *r=root->right;root->right=r->left;r->left=root;return r;}TreeNode* DoubleRotateWithLeft(TreeNode *&root){SingleRotateWithRight(root->left);return SingleRotateWithLeft(root);}TreeNode* DoubleRotateWithRight(TreeNode *&root){SingleRotateWithLeft(root->right);return SingleRotateWithRight(root);}void inorder(TreeNode *root){if(root){inorder(root->left);cout<<root->val<<" ";inorder(root->right);}} };int main() {Solution s;TreeNode *root=NULL;vector<int> vec={1,2,3,4,5,6,7,8,9,10};root=s.sortedArrayToBST(vec);s.inorder(root);cout<<endl;cout<<s.maxDepth(root)<<endl; }
Convert Sorted Array to Binary Search Tree相关推荐
- Convert Sorted Array to Binary Search Tree - LeetCode
目录 题目链接 注意点 解法 小结 题目链接 Convert Sorted Array to Binary Search Tree - LeetCode 注意点 不要访问空结点 题目要求的是平衡二叉搜 ...
- LeetCode: Convert Sorted Array to Binary Search Tree 解题报告
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 【数组递归构造二叉树】LeetCode 108. Convert Sorted Array to Binary Search Tree
LeetCode 108. Convert Sorted Array to Binary Search Tree Solution1:我的答案 构造二叉树利用递归 /*** Definition fo ...
- LeetCode: 108. Convert Sorted Array to Binary Search Tree
题目 Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- Convert Sorted Array to Binary Search Tree With Minimal Height
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...
- [LeetCode] Convert Sorted Array to Binary Search Tree
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Hi ...
- [LeetCode]Convert Sorted Array to Binary Search Tree
题目描述:(链接) Given an array where elements are sorted in ascending order, convert it to a height balanc ...
- LeetCode 108. Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- leetcode python3 简单题108. Convert Sorted Array to Binary Search Tree
1.编辑器 我使用的是win10+vscode+leetcode+python3 环境配置参见我的博客: 链接 2.第一百零八题 (1)题目 英文: Given an array where elem ...
最新文章
- Clang-Format: Visual Studio Style
- 11.条件语句if,switch
- stm32IAP代码升级小结
- 【STM32】DMA详解
- 一个简单的WebService服务
- win2012 ad用户和计算机,Server2012R2搭建AD域服务器并添加登录用户
- html5-样式表的使用-初步
- 【转载】谁动了摩卡的奶酪?
- CentOS7下安装并简单设置PostgreSQL笔记
- 放弃幻想,全面拥抱Transformer:自然语言处理三大特征抽取器(CNN/RNN/TF)比较...
- Java从入门到精通 第0章Java学习指南
- python3 shell,python3执行shell命令
- springboot球类运动教学网站的设计与实现271611
- 在企业中采用知识管理工具的好处
- Chrome书签同步方法
- 177.5. FAQ
- 计算机系表白祖国母亲,青春向祖国告白——“给祖国母亲的一封告白信”活动纪实...
- 从某一点出发沿任意一方向旋转矩阵计算思考与实现
- 屏蔽百度广告,百度新闻
- 文献综述-家装风格自动分类技术综述
热门文章
- 报错:ModuleNotFoundError: No module named ‘cv_bridge‘,以及在ROS是如何安装cv_bridge库包
- 写出现代计算机网络的三个方面的应用,《计算机应用基础》统考模拟试题一及参考答案0...
- vim中搭建与sourceinsight类似功能
- LeetCode Combinations (组合)
- UVa133 - The Dole Queue
- HDU2093 考试排名
- Netty学习笔记(一)
- python 学习
- Linux下源码编译安装Python3
- log_archive_dest_1设置报错