Convert Sorted List to Binary Search Tree

Total Accepted: 12283 Total
Submissions: 45910My Submissions

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

总结:这个是网上找的,还不明确为什么是以下这个样子,去掉&,就出错

ListNode *&node
TreeNode *sortedListToBST(ListNode *head)
{
int len = 0;
ListNode * node = head;
while (node != NULL)
{
node = node->next;
len++;
}
return buildTree(head, 0, len-1);
} TreeNode *buildTree(ListNode *&node, int start, int end)
{
if (start > end) return NULL;
int mid = start + (end - start)/2;
TreeNode *left = buildTree(node, start, mid-1);
TreeNode *root = new TreeNode(node->val);
root->left = left;
node = node->next;
root->right = buildTree(node, mid+1, end);
return root;
}

总结:这个開始的时候有点问题,改动了一下

TreeNode *sortedListToBST(ListNode *head)
{
vector<TreeNode*> treeNodes;
if(head == NULL) return NULL;
while (head != NULL)
{
TreeNode *node = new TreeNode(head->val);
treeNodes.push_back(node);
head = head->next;
}
return genBST(0, treeNodes.size()-1, treeNodes);
} TreeNode* genBST(int start, int end, vector<TreeNode*> &treeNodes)
{
if (start == end) return treeNodes[start];
else if (start+1 == end)
{
treeNodes[start]->right = treeNodes[end];
return treeNodes[start];
} int mid = (start+end)/2;
TreeNode* root = treeNodes[mid];
root->left = genBST(start, mid-1, treeNodes);
root->right = genBST(mid+1, end, treeNodes);
return root;
}

总结:上面这个代码跟我參考得到的思想一致,可是我的是time limit

TreeNode *sortedListToBST(ListNode *head) {
int len = 0;
ListNode *node = head;
while(node)
{
len++;
node = node->next;
}
return BuildTree(head,1,len);
} TreeNode *BuildTree(ListNode *node,int start,int end)
{
if(start > end) return NULL;
int mid = start + (end-start)/2;
ListNode *mi_node = node;
for(int i=1;i<mid;i++)
{
mi_node = mi_node->next;
} TreeNode *root = new TreeNode(mi_node->val);
root->left = BuildTree(node,start,mid-1);
root->right = BuildTree(node,mid+1,end);
return root;
}

leetcode__Convert Sorted List to Binary Search Tree的更多相关文章

  1. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  2. [LeetCode] Convert Sorted List to Binary Search Tree 将有序链表转为二叉搜索树

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  3. [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. 这道 ...

  4. leetcode -- Convert Sorted List to Binary Search Tree

    Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...

  5. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  6. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  7. 【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 ...

  8. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

  9. 34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree

    Convert Sorted List to Binary Search Tree OJ: https://oj.leetcode.com/problems/convert-sorted-list-t ...

随机推荐

  1. web design tools

    https://www.google.com/webdesigner/ http://html.adobe.com/edge/inspect/ http://www.creativebloq.com/ ...

  2. uva_1422 Processor

    题目链接 题意: 有n个任务, 每个任务要在规定的时间[l, r]内完成, 工作量为 w, 每个任务可以分开完成. 求, 使得所有任务都完成的最大速度的最小值. 思路: 最大值最小问题, 二分. 因为 ...

  3. WebApp触屏版网站开发要点

    所谓的触屏版网站其实也是WebApp的一种展示形式,主要是依赖HTML+CSS+Javascript这三个关键因素来实现,相比较原生客户端程序来说优点就是开发周期短.升级简单.维护成本低,因为从根本上 ...

  4. NAND Flash的基本操作——读、写、擦除

    基本操作 这里将会简要介绍一下NAND Flash的基本操作在NAND Flash内部是如何进行的,基本操作包括:读.写和擦除.   读:     当我们读取一个存储单元中的数据时(如图2.4),是使 ...

  5. JAVA 抛出与声明异常

    在编程过程中,我们往往会遇到这种情况,在当前环境中无法解决,比如用户传入的参数错误,IO设备问题等.此时,就要从当前环境中抛出异常提交给上级来处理. 在JAVA语言中,使用throw关键字来抛出异常. ...

  6. Serif和Sans-serif字体的区别(转)

    在西方国家罗马字母阵营中,字体分为两大种类:Sans Serif和Serif,打字机体虽然也属于Sans Serif,但由于是等宽字体,所以另外独立出Monospace这一种类,例如在Web中,表示代 ...

  7. 数据挖掘十大经典算法(5) 最大期望(EM)算法

    在统计计算中,最大期望(EM,Expectation–Maximization)算法是在概率(probabilistic)模型中寻找参数最大似然估计的算法,其中概率模型依赖于无法观测的隐藏变量(Lat ...

  8. 【HDOJ】1512 Monkey King

    左偏树+并查集.左偏树就是可合并二叉堆. /* 1512 */ #include <iostream> #include <string> #include <map&g ...

  9. 【HDOJ】2732 Leapin' Lizards

    贪心+网络流.对于每个结点,构建入点和出点.对于每一个lizard>0,构建边s->in position of lizard, 容量为1.对于pillar>0, 构建边in pos ...

  10. 基础算法(二分,贪心):NOIP 2012 疫情控制

    题目大意 给出一棵n个节点的树,根是1,要在除根节点以外的点建立检查点,使得从每条根到叶子的路径上都至少存在一个检查点.检查点由军队来建立.初始军队的位置是给定的,移动军队走一条边需要花费这条边的权值 ...