【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
标签(空格分隔): LeetCode
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/
题目地址:https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/description/
题目描述:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.
Example:
Given the sorted linked list: [-10,-3,0,5,9],
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST:
0
/ \
-3 9
/ /
-10 5
题目大意
把一个单链表转成二叉平衡树。二叉平衡树是左右子树的高度差最多为1.
解题方法
这个题目和108. Convert Sorted Array to Binary Search Tree基本一模一样啊!所以毫无疑问地,我把链表转成了数组,然后再用108题的做法就过了。
而且这个题神奇的地方在于,使用python2超时,但是使用Python3就通过了。以后还是尽量用python3比较好吧。。
哦对,python3要用地板除。
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sortedListToBST(self, head):
"""
:type head: ListNode
:rtype: TreeNode
"""
nums = []
while head:
nums.append(head.val)
head = head.next
return self.helper(nums)
def helper(self, nums):
if not nums: return None
_len = len(nums)
mid = _len // 2
root = TreeNode(nums[mid])
root.left = self.helper(nums[:mid])
root.right = self.helper(nums[mid+1:])
return root
日期
2018 年 6 月 23 日 ———— 美好的周末要从刷题开始
【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)的更多相关文章
- [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 ...
- Leetcode#109 Convert Sorted List to Binary Search Tree
原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...
- 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 ...
- Java for 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 ...
- 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 ...
- [leetcode]109. Convert Sorted List to Binary Search Tree链表构建二叉搜索树
二叉树的各种遍历方式都是可以建立二叉树的,例如中序遍历,就是在第一步建立左子树,中间第二步建立新的节点,第三步构建右子树 此题利用二叉搜索树的中序遍历是递增序列的特点,而链表正好就是递增序列,从左子树 ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告
原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...
随机推荐
- 入坑不亏!我们最终决定将 70w+ 核心代码全部开源
作者 | 一啸 来源 | 尔达 Erda 公众号 背景故事 2017 年初,我们基于 DC/OS (mesos + marathon) 开始构建端点自己的 PaaS 平台,核心任务就是解决公司的软件开 ...
- 论文解读(GraRep)《GraRep: Learning Graph Representations with Global Structural Information》
论文题目:<GraRep: Learning Graph Representations with Global Structural Information>发表时间: CIKM论文作 ...
- tomcat在eclipse上发布,Perference下的server找不到解决办法
help--->Install New software得到如下所示 下面work with选项的内容与你的eclipse版本有关 我的eclipse版本为eclipse-java-2019-0 ...
- Linux学习 - 文件特殊权限
一.SUID权限(只针对文件) 只有可执行的二进制程序才能设定SUID权限 命令执行者要对该程序拥有x(执行)权限 1 拥有SUID的文件 /usr/bin/passwd 2 功能: 命令执行者(其他 ...
- Linux学习 - 挂载命令
一.mount 1 功能 将外设手工挂载到目标挂载点 2 语法 mount [-t 文件系统] [设备文件名] [挂载点] 3 范例 mkdir /mnt/cdrom 在/mnt下创建一个cd ...
- OC-copy,单例
总结 编号 主题 内容 一 NSFileManager NSFileManager介绍/用法(常见的判断)/文件访问/文件操作 二 集合对象的内存管理 集合对象的内存管理/内存管理总结 三 *copy ...
- 【Linux】【Commands】基础概念及常用基础命令
命令的语法通用格式: ------------------------------------------------ #COMMAND OPTIONS ARGUMENTS 发起命令:请求内核将某个二 ...
- Linux 双网卡绑定及Bridge
Linux 双网卡绑定及Bridge 阅读(5,202) 一:linux操作系统下双网卡绑定有七种模式.现在一般的企业都会使用双网卡接入,这样既能添加网络带宽,同时又能做相应的冗余,可以说是好处多多. ...
- django搭建示例-ubantu环境
python3安装--------------------------------------------------------------------------- 最新的django依赖pyth ...
- SpringCloud微服务服务间调用之OpenFeign介绍
开发微服务,免不了需要服务间调用.Spring Cloud框架提供了RestTemplate和FeignClient两个方式完成服务间调用,本文简要介绍如何使用OpenFeign完成服务间调用. Op ...