题目:

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

代码:

/**
* 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;
// p2 point to the pre node of mid
ListNode dummy(-);
dummy.next = head;
ListNode *p1 = &dummy, *p2 = &dummy;
while ( p1 && p1->next && p1->next->next ) { p1 = p1->next->next; p2 = p2->next;}
// get the mid val & cut off the mid from linked list
int val = p2->next->val;
ListNode *h2 = p2->next ? p2->next->next : NULL;
p2->next = NULL;
// recursive process
TreeNode *root = new TreeNode(val);
root->left = Solution::sortedListToBST(dummy.next);
root->right = Solution::sortedListToBST(h2);
return root;
}
};

tips:

1. 沿用跟数组一样的思路,采用二分查找

2. 利用快慢指针和虚表头技巧;最终的目的是p2指向mid的前驱节点。

3. 生成该节点,并递归生成left和right (这里需要注意传递的是dummy.next而不是head,原因是如果链表中只有一个节点,传head就错误了)

============================================

第二次过这道题,熟练了一些。重点在于求ListNode的中间节点。

/**
* 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) return new TreeNode(head->val);
ListNode* p1 = head;
ListNode* pre = p1;
ListNode* p2 = head;
while ( p2 && p2->next )
{
pre = p1;
p1 = p1->next;
p2 = p2->next->next;
}
TreeNode* root = new TreeNode(p1->val);
pre->next = NULL;
root->left = Solution::sortedListToBST(head);
root->right = Solution::sortedListToBST(p1->next);
return root;
}
};

【Convert Sorted List to Binary Search Tree】cpp的更多相关文章

  1. 【Convert Sorted Array to Binary Search Tree】cpp

    题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...

  2. leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现

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

  3. 【二叉查找树】05根据升序的链表构造二叉查找树【Convert Sorted List to Binary Search Tree】

    利用递归,构造二叉查找树, ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给一个 ...

  4. 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...

  5. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

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

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

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

随机推荐

  1. iOS动画 三维透视投影 m34

    transform的结构如下:struct CATransform3D{  CGFloat m11, m12, m13, m14;  CGFloat m21, m22, m23, m24;  CGFl ...

  2. Windows下WEB服务器的选择与搭建

    本文主要基于支持perl的web服务器的选择. 一直基于web开发,服务器都是linux下使用webmin搭建的,惭愧的说一句,这么多年,也好好研究过WEB服务器,单从这个角度,是不是可以反应出web ...

  3. Windows 2008 R2 X64 安装WebsitePanel(WSP虚拟主机管理面板)

                   Windows 2008 R2 X64  安装WebsitePanel(WSP2.0虚拟主机管理面板) 估计很多同学都还不知道WebsitePanel是什么东东吧,Web ...

  4. ADO.NET中ExcuteNonQuery获取存储过程Return返回值

    /// <summary> /// 获取当月用户已投票数量 /// </summary> /// <param name="userId">用户 ...

  5. Win+R快速打开你的应用程序

    参考自:http://blog.csdn.net/nothing0318/article/details/7179405 1:在你的磁盘任意位置创建一个文件夹,比如C:MyShortcut,然后将你的 ...

  6. rails笔记

    rake -T 列出全部taskconfig.active_record.schema_format = :sql #remove the old db/schema.rb file, create ...

  7. 安装使用rspec

    一,安装ruby. 二,运行命令,安装rspec的gem包: gem install rspec 会看到如下的结果: Fetching: rspec-core-2.14.7.gem (100%) Fe ...

  8. hdu 4631Sad Love Story<计算几何>

    链接:http://acm.hdu.edu.cn/showproblem.php?pid=4631 题意:依次给你n个点,每次求出当前点中的最近点对,输出所有最近点对的和: 思路:按照x排序,然后用s ...

  9. oracle 修改表空间存储路径

    [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 Oracle Database 11g Enterpris ...

  10. hdu 5412 CRB and Queries

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5412 CRB and Queries Description There are $N$ boys i ...