Convert Sorted List to Balanced Binary Search Tree leetcode
题目:将非递减有序的链表转化为平衡二叉查找树!
参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643
利用递归思想:首先找到链表的中间节点,于是链表被分为了由该中间节点划分开来的两部分。递归地处理这两部分,最终便得到了平衡二叉查找树。
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* sortedListToBST(ListNode* head) {
if(!head)
return NULL;
if(head->next==NULL){
TreeNode*root=new TreeNode(head->val);
return root;
}
ListNode*pre=getLeftOfMid(head),*mid=pre->next;
TreeNode*root=new TreeNode(mid->val);//根
pre->next=NULL;
root->left=sortedListToBST(head);//左子树
root->right=sortedListToBST(mid->next);//右子树
return root;
}
ListNode* getLeftOfMid(ListNode*head)//说简单点,就是获取链表中间节点,作为树的根节点,并由此划分根的2个子树
{
if(!head)
return NULL;
ListNode*pre=head,*back=head;
while(back!=NULL)//back后退的步数大致是head的两倍,确保pre能落在中间节点位置的前一结点处
{
back=back->next;
if(!back)
break;
back=back->next;
if(!back)
break;
pre=head;
head=head->next;
}
return pre;
}
};
上面的方法是一种自顶向下的方法,先找到root然后对左右子树分别递归调用。
网上又看到一种自底向上的方法,算法复杂度为O(N)。先递归构建左子树,在构建左子树的同时不断移动链表的头指针,链表的头指针永远是对应当前子树位置的。一直到左叶子节点,左叶子节点对应的就是链表的第一个元素,生成左叶子节点之后移动链表当前指针。
看这个博客:http://blog.csdn.net/linhuanmars/article/details/23904937
这里的问题是对于一个链表我们是不能常量时间访问它的中间元素的。这时候就要利用到树的中序遍历了,按照递归中序遍历的顺序对链表结点一个个进行访问,而我们要构造的二分查找树正是按照链表的顺序来的。思路就是先对左子树进行递归,然后将当前结点作为根,迭代到下一个链表结点,最后在递归求出右子树即可。整体过程就是一次中序遍历,时间复杂度是O(n),空间复杂度是栈空间O(logn)。
来看一下对应数组的中序遍历建立BST代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
int cur;//不去掉这前面的static编译不会通过的
public:
TreeNode* sortedArrayToBST(vector<int>& nums) {
cur = ;
if (nums.size() == )
return NULL;
return helper(nums, , nums.size() - );
}
TreeNode* helper(vector<int>&nums, int l, int r)
{
if (l > r)
return NULL;
int mid = (l+r) / ;
TreeNode*left=helper(nums, l, mid-);
TreeNode * root = new TreeNode(nums[cur++]);//回忆考研时候,中序遍历中,这个地方才是真正干活的语句(并且终须遍历二叉排序树得到的就是有序数组)
root->left = left;
root->right = helper(nums, mid + , r);
return root;
}
};
Convert Sorted List to Balanced Binary Search Tree leetcode的更多相关文章
- Convert Sorted List to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list ...
- Convert Sorted Array to Balanced Binary Search Tree (BST)
(http://leetcode.com/2010/11/convert-sorted-array-into-balanced.html) Given an array where elements ...
- Convert Sorted Array to Binary Search Tree leetcode java
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- Validate Binary Search Tree [LeetCode]
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Recover Binary Search Tree [LeetCode]
Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...
- Validate Binary Search Tree——LeetCode
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- Recover Binary Search Tree leetcode java
题目: Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without chan ...
- Validate Binary Search Tree leetcode java
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Lowest Common Ancestor of a Binary Search Tree -- LeetCode
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- direction
基本上,大家只要关心下面这两个属性值就好了: direction: ltr; // 默认值 direction: rtl; 其中,ltr是初始值,表示left-to-right,就是从左往右的意思,再 ...
- 分区时"磁盘上没有足够的空间完成此操作"的解决方法
在新的预装windows 7的品牌机上,工作人员一般将磁盘分为C.D两个分区.但往往造成C盘有很大一部分的空间没办法分出来,而分出来的部分空间又不能和后面的磁盘合并,甚至出现无法新建简单卷的操作,即点 ...
- 查看mysql状态
命令:mysqladmin -uroot -p -h172.16.0.20 status Uptime: 14317755 Threads: 61 Questions: 187732924 Sl ...
- UVa 11040 Add bricks in the wall (水题递推)
题意:给定一个金字塔,除了最后一行,每个数都等于支撑它的两个数的和,现在给奇数行的左数奇数位置,求整个金字塔. 析:很容易看出来,从下往上奇数行等于 a[i][j] = (a[i-2][j-1] - ...
- Android 实用技巧 --- 命令godir (转载)
转自:http://blog.csdn.net/bigmarco/article/details/6995426 source build/envsetup.sh后可以使用很多android集成的sh ...
- nodejs实现百度实时推送
想要加快百度收录,肯定免不了链接提交吧,当然链接提交的方式有很多种,今天来说一下百度的实时推送.. 第一次看到这post请求确实有点萌逼,我自己是做前端的对后台接触确实不多,见到的前端发送post请求 ...
- C++笔试题(三)
普天是南京一家通信公司,全称为:南京普天通信股份有限公司,公司网址为:http://www.postel.com.cn 网上流传一套普天C++笔试题,我将我做的答案公布与此,仅供参考. 1.实现双向链 ...
- hdoj5832【模拟】
主要还是一个10001的倍数的问题: 队友的思路: 01 1个数*10001,最后四位是这个数的后四位 比如 521456 10001 521456 521456 9 5215081456 从后面fo ...
- 两年Java程序员面试经验分享,从简历制作到面试总结!
前言 工作两年左右,实习一年左右,正式工作一年左右,其实挺尴尬的,高不成低不就.因此在面试许多公司,找到了目前最适合自己的公司之后.于是做一个关于面试的总结.希望能够给那些依旧在找工作的同学提供帮助. ...
- LuoguP1342请柬 【最短路/建反图】By cellur925
题目传送门 开始就想直接正向跑一遍Dij把到各点的最短路加起来即可,后来发现与样例少了些,于是再读题发现需要也求出学生们回来的最短路. 但是注意到本题是有向图,如果是无向图就好说. 那么我们怎么解决? ...