Problem Link:

http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/

We design a auxilar function that convert a linked list to a node with following properties:

  1. The node is the mid-node of the linked list.
  2. The node's left child is the list consisting of the list nodes before the mid-node.
  3. The node's right child is the list consisting of the list nodes after the mid-node.

The algorithm will convert the linked lists and create the tree nodes level by level until there is no linked list existing as the new-created node's children.

The python code is as follows.

# Definition for a  binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None class Solution:
# @param head, a list node
# @return a tree node
def sortedListToBST(self, head):
if not head:
return None
root = self.find_mid(head)
q = [root]
while q:
new_q = []
for n in q:
if n.left:
n.left = self.find_mid(n.left)
new_q.append(n.left)
if n.right:
n.right = self.find_mid(n.right)
new_q.append(n.right)
q = new_q
return root def find_mid(self, head):
prev = None
slow = head
fast = head
while True:
# Fast go one step
if fast.next:
fast = fast.next
else:
break
# Slow go one step
prev = slow
slow = slow.next
# Fast go another step
if fast.next:
fast = fast.next
else:
break
# Create a TreeNode for the mid node pointed by 'slow'
node = TreeNode(slow.val)
# Set left and right children
if prev:
node.left = head
prev.next = None
else:
node.left = None
node.right = slow.next
# Return the node
return node

【LeetCode OJ】Convert Sorted List to Binary Search Tree的更多相关文章

  1. 【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 ...

  2. LeetCode OJ 108. Convert Sorted Array to Binary Search Tree

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 把一 ...

  3. LeetCode OJ 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 ...

  4. LeetCode OJ: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 ...

  5. LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 讲一 ...

  6. 【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 ...

  7. 【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 ...

  8. LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder

    1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in ...

  9. Leetcode No.108 Convert Sorted Array to Binary Search Tree(c++实现)

    1. 题目 1.1 英文题目 Given an integer array nums where the elements are sorted in ascending order, convert ...

随机推荐

  1. python之rabbitMQ篇

    一.RabbitMQ安装 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统,它遵循Mozilla Pulic License开源协议. MQ全称为Message Queue,消息队列 ...

  2. SQL SA密码丢失

    ------------记不清了,以下似乎是这样操作的-----NET STOP MSSQLSERVER Net Start MSSQLServer /m"SQLCMD" 安装并使 ...

  3. GMF:如何在不打开Editor的情况下生成图片

    问题 GMF应用中,有时我们希望在不打开*DiagramEditor的情况下,从文件就能生成它的图片   解决方案 首先,从文件中构造DiagramImpl实例: TransactionalEditi ...

  4. [poj3017] Cut the Sequence (DP + 单调队列优化 + 平衡树优化)

    DP + 单调队列优化 + 平衡树 好题 Description Given an integer sequence { an } of length N, you are to cut the se ...

  5. 关于Extjs MVC模式上传文件的简单方式

    Extjs新手研究上传文件的事情估计是件很头痛的问题,毕竟,我就在头痛.最近两天一直在忙文件上传问题,终于小有收获. 用的是Extjs+MVC3.0+EF开发,语言为C#.前台window代码显示列内 ...

  6. Linux命令基本格式及目录处理命令

    命令提示符 [root@localhost ~]# root:当前登录用户 localhost:主机名 ~:当前所在的目录,此处为"家"目录 #:root超级用户的提示符,如果是普 ...

  7. 《第一本docker书》第4章 使用docker镜像和仓库 读书笔记

    docker最底端是一个引导文件系统,即bootfs. 第二层是root文件系统rootfs,位于引导文件系统之上. 在传统的Linux引导过程中,root文件系统会最先以只读的方式加载,当引导结束并 ...

  8. iOS核心动画学习整理

    最近利用业余时间终于把iOS核心动画高级技巧(https://zsisme.gitbooks.io/ios-/content/chapter1/the-layer-tree.html)看完,对应其中一 ...

  9. pytessact 出现Error2错误

    x pytessact安装后不起作用 一.没有安装tessact-ocr 点击下载,安装 http://jaist.dl.sourceforge.net/project/tesseract-ocr-a ...

  10. mysql 登录及常用命令

    一.mysql服务的启动和停止 mysql> net stop mysql mysql> net start mysql 二.登陆mysql mysql> 语法如下: mysql - ...