题目:

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. ie使用firebug

    在网页插入以下代码即可. <script type="text/javascript" src="http://getfirebug.com/releases/li ...

  2. IOS绘图——简单三角形

    #import <UIKit/UIKit.h> @interface MyView : UIView @end #import "MyView.h" @implemen ...

  3. vs C#数据库导入EXCLE

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

  4. Knockout.Js官网学习(监控属性Observables)

    前言 1.创建一个ViewModel <script type="text/javascript"> //1.创建一个ViewModel var myViewModel ...

  5. 一些CSS"bug"

    1.img三像像素问题 解决办法:img{display:block;} or img{vertical-align:middle;} 问题原因:img是行内元素,默认的垂直对齐方式 baseline ...

  6. C++primer 阅读点滴记录(三)

    14章 操作符重载和转换 重载操作符是具有特殊名称的函数:保留字operator后接需要定义的操作符符号. 1.重载的操作符名: + – * / % ^ & | ~ ! , = <  & ...

  7. UltraEdit中使用正则表达式

    正则表达式 (UltraEdit Syntax): % 匹配行首 - 表明要搜索的字符串一定在行首. $ 匹配行尾 - 表明要搜索的字符串一定在行尾 ? 匹配除换行符外的任一单个字符. * 匹配任意个 ...

  8. 对于java反射的理解

    java中的反射是一种强大的工具,它能够创建灵活的代码,这些代码可以在运行时装配,无序在组件之间进行链接. 反射允许在编写与执行时,使程序代码能够接入装载到JVM的类的内部信息,而不是源代码中选定的类 ...

  9. ARM中MMU地址转换理解

    首先,我们要分清ARM CPU上的三个地址:虚拟地址(VA,Virtual Address).变换后的虚拟地址(MVA,Modified Virtual Address).物理地址(PA,Physic ...

  10. linux内核SPI总线驱动分析(二)(转)

    简而言之,SPI驱动的编写分为: 1.spi_device就构建并注册  在板文件中添加spi_board_info,并在板文件的init函数中调用spi_register_board_info(s3 ...