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 ...
随机推荐
- Ubuntu 16.04 如何使用Samba服务器
对于Windows与Ubuntu之间的数据传输,我们习惯于使用FTP工具,不过还是有学员问到samba服务器搭建和使用的问题,这便是本文的来由. Ubuntu版本:ARM裸机1期加强版配套的Ubunt ...
- webpack项目调试以及独立打包配置文件
webpack项目调试 -sourcemap webpack配置提供了devtool这个选项,如果设置为 ‘#source-map’,则可以生成.map文件,在chrome浏览器中调试的时候可以显示源 ...
- TRACE 学习
TRACE 宏有点象我们以前在C语言中用的Printf函数,使程序在运行过程中输出一些调试信息,使我们能了解程序的一些状态.在Output中可以查看到结果. 但有一点不同的是:TRACE 宏只 ...
- LRESULT 数据类型
MSDN: Signed result of message processing. This type is declared in WinDef.h as follows: typedef LON ...
- View Controller Programming Guide for iOS---(七)---Resizing the View Controller’s Views
Resizing the View Controller’s Views A view controller owns its own view and manages the view’s cont ...
- k8s-部署dashboard1.10.1-十七
一.获取镜像和填坑 我的k8s是1.13.1,这里dashboard用的1.10.1: 由于国内不能访问Google,而且大部分人可能也没有其他途径访问:只能在阿里云或者其他镜像网站上获取了: 镜像获 ...
- mysql 分区后查询效率
准备工作: 蠕虫复制 文章表 增加数据到112万 语法:insert into tableNameA select * from tableNameB 未分区查询 54s 改变现有表 ...
- C#拷贝整个文件夹以及子目录和其中文件
private void CopyDirectory(string srcPath, string desPath) { string folderNam ...
- 【SCOI2016】Day1 模拟
2018.8.16 8:00~11:06 先看t1,成功读错题... 以为是一个字符串的所有后缀都得在计划表里,否则权值就得是$n^2$ 然后花了一个小时多一点写了一个错误的做法 然后没有分 才发现看 ...
- 模拟+位运算 HDOJ 5491 The Next
题目传送门 题意:意思很简单,找一个最接近D且比D大的数,满足它的二进制表示下的1的个数在[S1, S2]之间 分析:从D + 1开始,若个数小于S1,那么从低位向高位把0替换成1直到S1就是最小值, ...