【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 ...
随机推荐
- 深入了解Javascript模块化编程
本文译自Ben Cherry的<JavaScript Module Pattern: In-Depth>.虽然个人不太认同js中私有变量存在的必要性,但是本文非常全面地介绍了Javascr ...
- jQuery中$(function() {});问题详解
$(function() {});是$(document).ready(function(){ })的简写,最早接触的时候也说$(document).ready(function(){ })这个函数是 ...
- div+css+jQuery图片横向滚动代码(带左右点击按钮)
首先感谢Blue老师的javascript教程,给了我很多的启发,这是我在看完10 - 定时器的使用 - 2这节视频后,自己试着用jQuery重新改写了一下代码,感觉至少比百度搜出来的那一坨靠谱多了, ...
- mybatis动态sql中的trim标签的使用(转)
trim标记是一个格式化的标记,可以完成set或者是where标记的功能,如下代码: 1. select * from user <trim prefix="WHERE" p ...
- WebService IIS 部署
刚刚开始接触c#怎么发布程序都搞不定,经过查看网上同仁的资料,自己写了个Webservice发布了下 1.首先自己写好程序,鼠标选中所写Webservice程序,单击鼠标右键-------->发 ...
- 异步导出excel
最近看园里有几篇写有关导出导入excel的博客,我正好最近在项目中也有涉及想来一起分享一下,正好整理一下自己的思路. 一.异步的方式是通过iframe来实现,代码如下: if ($('#downloa ...
- dump buffer cache
1.基础内容: ALTER SESSION SET EVENTS 'immediate trace name buffers level n'; n取值意义: 1 只转储buffer header. ...
- ORA-01207: file is more recent than control file -
OS: [root@yoon ~]# more /etc/oracle-releaseOracle Linux Server release 5.7 DB: Oracle Database 11g E ...
- Oracle出现字符集问题处理方法
1. Cmd进去DOS 2. 再输入dbca(database create) 3. 弹出的界面,直接下一步,选择删除数据库 4. 成功删除后,回到一第一界面,选择创建数据库,下一步. 5. ...
- JavaScript高级程序设计之Date类型
ECMAScript 中的 Date 类型是在早期 Java 的 java.util.Date 类基础上构建的. Date 类型使用自 UTC (国际协调时间)1970年1月1日午夜(零时)开始经过的 ...