题目:

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. 面向初学者的 Windows 10 UWP 应用开发

    眼看 Windows 10 for Mobile 正式版也快要推送了,就先挖个坑吧,原文视频链接为:Windows 10 development for absolute beginners,以下博客 ...

  2. IIS服务器环境配置(一)

    在开始-> 控制面板 ->打开或关闭功能 IIS 服务器程序 勾选  HTML勾选 完成添加后  重启  确认是否添加上在控制面板的 管理工具中查看

  3. POJ C++程序设计 编程题#1 编程作业—STL1

    编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...

  4. 软件工程 speedsnail 第二次冲刺3

    20150520 完成任务:划线第三天,能画出一条直黄线且与蜗牛共存: 遇到问题: 问题1 碰撞检测有缺陷 解决1 没有解决 明日任务: 实现蜗牛与线的碰撞

  5. winform自动添加同级目录下可执行文件的快捷方式到右键菜单中

    /// <summary> /// 追加同目录下可执行文件到右键菜单中 /// 在form的Load事件中调用:new clsContextMenuStrip(this.FindForm( ...

  6. Bootstrap Alert Auto Close

    http://stackoverflow.com/questions/23101966/bootstrap-alert-auto-close http://jsfiddle.net/mfX57/ $( ...

  7. nginx+php与apache+php性能对比

    测试工具http_load相同的动态页面测试,相同的硬件资源,相同并发,相同请求数量的前提下,nginx+php比apache+php的性能要 差,而且如果请求的压力大于硬件资源的承受能力,nginx ...

  8. android 的通知管理

    1在context里定义通知管理器(NotificationManager) NotificationManager notificationManager = (NotificationManage ...

  9. ajax 注意点

    1. 为了把数据发送到服务器,应该使用POST 方法,为了从服务器检索数据,应该使用GET 方法. 2.ajax 完整参数, url  发送的地址 http://localhost/default.a ...

  10. Python Ogre Blender(转载)

    http://www.cppblog.com/Charlib/archive/2010/05/31/python_ogre_blender_1.html PyOgre入门以及如何使用Blender制作 ...