【Convert Sorted List to Binary Search Tree】cpp
题目:
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的更多相关文章
- 【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 ...
- 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 ...
- 【二叉查找树】05根据升序的链表构造二叉查找树【Convert Sorted List to Binary Search Tree】
利用递归,构造二叉查找树, ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给一个 ...
- 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...
- 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- 面向初学者的 Windows 10 UWP 应用开发
眼看 Windows 10 for Mobile 正式版也快要推送了,就先挖个坑吧,原文视频链接为:Windows 10 development for absolute beginners,以下博客 ...
- IIS服务器环境配置(一)
在开始-> 控制面板 ->打开或关闭功能 IIS 服务器程序 勾选 HTML勾选 完成添加后 重启 确认是否添加上在控制面板的 管理工具中查看
- POJ C++程序设计 编程题#1 编程作业—STL1
编程题#1 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 下面的程序输出结 ...
- 软件工程 speedsnail 第二次冲刺3
20150520 完成任务:划线第三天,能画出一条直黄线且与蜗牛共存: 遇到问题: 问题1 碰撞检测有缺陷 解决1 没有解决 明日任务: 实现蜗牛与线的碰撞
- winform自动添加同级目录下可执行文件的快捷方式到右键菜单中
/// <summary> /// 追加同目录下可执行文件到右键菜单中 /// 在form的Load事件中调用:new clsContextMenuStrip(this.FindForm( ...
- Bootstrap Alert Auto Close
http://stackoverflow.com/questions/23101966/bootstrap-alert-auto-close http://jsfiddle.net/mfX57/ $( ...
- nginx+php与apache+php性能对比
测试工具http_load相同的动态页面测试,相同的硬件资源,相同并发,相同请求数量的前提下,nginx+php比apache+php的性能要 差,而且如果请求的压力大于硬件资源的承受能力,nginx ...
- android 的通知管理
1在context里定义通知管理器(NotificationManager) NotificationManager notificationManager = (NotificationManage ...
- ajax 注意点
1. 为了把数据发送到服务器,应该使用POST 方法,为了从服务器检索数据,应该使用GET 方法. 2.ajax 完整参数, url 发送的地址 http://localhost/default.a ...
- Python Ogre Blender(转载)
http://www.cppblog.com/Charlib/archive/2010/05/31/python_ogre_blender_1.html PyOgre入门以及如何使用Blender制作 ...