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;
int size = ;
ListNode * listNode = head->next;
while(listNode)
{
size++;
listNode = listNode->next;
}
TreeNode* root = new TreeNode();
binarySearch(head,size,root);
return root;
}
void binarySearch(ListNode *head,int size, TreeNode* treeNode) //二分法,对于Array,需要一头一尾两个指针;对于List需要头指针及size
{
if(size == )
{
treeNode->val = head->val;
return;
}
ListNode * listNode = head;
int mid = size>>;
int i;
for(i = ; i<mid;i++)
{
listNode = listNode ->next;
}
treeNode->val = listNode->next->val;
treeNode->left = new TreeNode(); //先申请好空间,再传递。否则在被调用函数中指针本身值的变化并不会影响调用者
binarySearch(head,mid,treeNode->left);
if(size>)
{
treeNode->right = new TreeNode();
binarySearch(listNode->next->next,size-mid-,treeNode->right);
}
}
};

也可以通过引用传递,这样就不需要先初始化。

/**
* 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;
int size = ;
ListNode * listNode = head->next;
while(listNode)
{
size++;
listNode = listNode->next;
}
TreeNode* root = new TreeNode();
binarySearch(head,size,root);
return root;
}
void binarySearch(ListNode *head,int size, TreeNode* &treeNode) //按引用传递参数
{
if(size == )
{
treeNode=new TreeNode(head->val);
return;
}
ListNode * listNode = head;
int mid = size>>;
for(int i = ; i<mid;i++)
{
listNode = listNode ->next;
}
treeNode = new TreeNode (listNode->next->val); //在被调用处申请空间
binarySearch(head,mid,treeNode->left); //引用的是0x0000的地址
if(size>)
{
binarySearch(listNode->next->next,size-mid-,treeNode->right);
}
}
};

注意,NULL是一个宏定义

#define NULL 0

即NULL = (void*) 0

109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)的更多相关文章

  1. 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树

    108. Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascendin ...

  2. leetcode 108. Convert Sorted Array to Binary Search Tree 、109. Convert Sorted List to Binary Search Tree

    108. Convert Sorted Array to Binary Search Tree 这个题使用二分查找,主要要注意边界条件. 如果left > right,就返回NULL.每次更新的 ...

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

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

  4. Leetcode#109 Convert Sorted List to Binary Search Tree

    原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...

  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 把有序链表转成二叉搜索树

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

  7. leetcode 109 Convert Sorted List to Binary Search Tree ----- java

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

  8. 109. Convert Sorted List to Binary Search Tree

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

  9. 【Leetcode】109. Convert Sorted List to Binary Search Tree

    Question: Given a singly linked list where elements are sorted in ascending order, convert it to a h ...

随机推荐

  1. elixir 集成ejabberd

    备注: 我开发测试的环境时centos 1. 预备环境 1. openssl yum install -y  openssl-devel 2. xml yum install -y expat-dev ...

  2. springboot中 简单的SpringMvc全局异常处理

    1.全局异常处理类:GlobalExceptionHandler.java package com.lf.exception; import java.util.HashMap; import jav ...

  3. 请求MWS报错401:Access Denied

    跑MWS接口,报错: Caught Exception: Access denied Response Status Code: Error Code: AccessDenied Error Type ...

  4. 编译openvpn在链接时报tuncfg错误

    1. 链接时产生tuncfg错误 init.o: In function `do_persist_tuntap': init.c:(.text+0x24d8): undefined reference ...

  5. struts2学习(12)struts2验证框架2.自定义验证

    一.例子需求: 对敏感词进行验证: 将struts包中的validators.xml文件拷贝一份到src目录下,在最后面添加自己的验证器: com.cy.validators.SensitiveWor ...

  6. 在Mac下配置php开发环境:Apache+php+MySql (卡在 给mysql 设置不了账号密码)

    https://my.oschina.net/joanfen/blog/171109#OSC_h4_3 cmd 进入mysql的方法

  7. Java上传下载excel、解析Excel、生成Excel

    在软件开发过程中难免需要批量上传与下载,生成报表保存也是常有之事,最近集团门户开发用到了Excel模版下载,Excel生成,圆满完成,对这一知识点进行整理,资源共享,有不足之处还望批评指正,文章结尾提 ...

  8. Mac运维安装软件

    Maccrt软件 sudo spctl --master-disable 打开软件,复制到app,按照sn.txt输入即可 sudo spctl --master-enable crt快捷键crtl ...

  9. opatch lsinventory –details

    今天把RAC的数据库升完级后,在RAC1节点执行opatch lsinventory –detail 命令,没有报错.在rac2节点执行报错: [oracle@rac2 ~]$ opatch lsin ...

  10. 排除maven jar冲突 maven tomcat插件启动报错 filter转换异常

    最近在搞一个ssm+shiro的整合 用的maven tomcat插件 启动的时候报错,提示 maven org.springframework.web.filter.CharacterEncodin ...