LintCode 把排序数组转换为高度最小的二叉搜索树
来源:互联网 发布:c语言随机数生成 编辑:程序博客网 时间:2024/05/22 01:31
1.描述
给一个排序数组(从小到大),将其转换为一棵高度最小的排序二叉树。
注意事项
There may exist multiple valid solutions, return any of them.
样例
给出数组 [1,2,3,4,5,6,7]
, 返回
4 / \ 2 6 / \ / \1 3 5 7
2.分析
最初我没有注意到这是一颗二叉搜索树,因此按照层次遍历的方式从数组中依次取出一个值
建立新的节点,但是二叉搜索树必须要右子树大于左子树的节点值,因此把数组从中间分开
中间的值为节点值左半部分为左子树的节点值右半部分为右子树的节点值。
3.代码
/**
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param A: A sorted (increasing order) array
* @return: A tree node
*/
TreeNode *dfs(vector<int> &A,int a,int b)
{
if(a>b) return NULL;
int c=(a+b)/2;
TreeNode *root=new TreeNode(A[c]);//取当前ab段中间位置为当前节点值
root->left=dfs(A,a,c-1);//ab段的前半段为新ab段,节点为当前节点的左孩子
root->right=dfs(A,c+1,b);//ab段的后半段为新ab段,节点为当前节点的右孩子
return root;
}
TreeNode *sortedArrayToBST(vector<int> &A) {
// write your code here
if(A.size()==0) return NULL;
int a=0;
int b=A.size()-1;
int c=(a+b)/2;
TreeNode *root=new TreeNode(A[int(A.size()/2)]);
root->left=dfs(A,a,c-1);
root->right=dfs(A,c+1,b);
return root;
}
};
* Definition of TreeNode:
* class TreeNode {
* public:
* int val;
* TreeNode *left, *right;
* TreeNode(int val) {
* this->val = val;
* this->left = this->right = NULL;
* }
* }
*/
class Solution {
public:
/**
* @param A: A sorted (increasing order) array
* @return: A tree node
*/
TreeNode *dfs(vector<int> &A,int a,int b)
{
if(a>b) return NULL;
int c=(a+b)/2;
TreeNode *root=new TreeNode(A[c]);//取当前ab段中间位置为当前节点值
root->left=dfs(A,a,c-1);//ab段的前半段为新ab段,节点为当前节点的左孩子
root->right=dfs(A,c+1,b);//ab段的后半段为新ab段,节点为当前节点的右孩子
return root;
}
TreeNode *sortedArrayToBST(vector<int> &A) {
// write your code here
if(A.size()==0) return NULL;
int a=0;
int b=A.size()-1;
int c=(a+b)/2;
TreeNode *root=new TreeNode(A[int(A.size()/2)]);
root->left=dfs(A,a,c-1);
root->right=dfs(A,c+1,b);
return root;
}
};
4.总结
这道题我想到凌晨1点半,从123画到123456789,但结果还是不尽人意(/(ㄒoㄒ)/~~)。
其中一个致命的错误为我想表示a和b中间的值为(b-a)/2,但实际上b-a是这一段的长度,
并不能表示在数组中的位置,应该为(a+b)/2(a+(b-a)/2)。设一段数组开头为a,结尾
为b,则他的左半段开头为a,结尾为(a+b)/2;右半段开头为(a+b)/2,结尾为b。通过不断
二分数组得到左右节点的节点值。
0 0
- LintCode SortedArrayToBST 把排序数组转换为高度最小的二叉搜索树
- lintcode-把排序数组转换为高度最小的二叉搜索树-177
- 把排序数组转换为高度最小的二叉搜索树(LintCode)
- LintCode:把排序数组转换为高度最小的二叉搜索树
- lintCode(177)——把排序数组转换为高度最小的二叉搜索树
- LintCode:把排序数组转换为高度最小的二叉搜索树
- lintcode,把排序数组转换为高度最小的二叉搜索树
- LintCode | 177. 把排序数组转换为高度最小的二叉搜索树
- LintCode 把排序数组转换为高度最小的二叉搜索树
- 把排序数组转换为高度最小的二叉搜索树-LintCode
- lintcode——把排序数组转换为高度最小的二叉搜索树
- lintcode把排序数组转换为高度最小的二叉搜索树
- LintCode-把排序数组转换为高度最小的二叉搜索树
- lintcode 把排序数组转换为高度最小的二叉搜索树
- LintCode 把排序数组转换为高度最小的二叉搜索树
- LintCode 177-把排序数组转换为高度最小的二叉搜索树
- 把排序数组转换为高度最小的二叉搜索树-LintCode
- [LintCode 177] 把排序数组转换为高度最小的二叉搜索树(Python)
- 最长上升子序列
- CPP_Basic_Code_P8.1-PP8.8.7
- Qt类内部有一个该类类型指针
- Selenium自动化测试框架的搭建 (转)
- linux基本语法7
- LintCode 把排序数组转换为高度最小的二叉搜索树
- Linux操作系统使用及linux常用命令
- dom4j
- 对JVM的理解
- HDU1257 最少拦截系统 —— LIS
- 延迟初始化,双重检查加锁的陷阱
- 平衡二叉查找树插入节点操作( AVLTree ):旋转、调整平衡
- MySQL
- poj