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. WEB前端的原理及组成

    1:认识WEB前端的组成部分和相关专业术语!具体的总结如下:

  2. 用webdriver+phantomjs实现无浏览器的自动化过程

    环境准备 1. 安装python: 2. 安装pip: 3. 通过pip安装selenium: 4. 下载phantomJS的包并解压缩: 1. 若在Windows系统中,将下载的phantomjs文 ...

  3. double函数和int函数

    可以看到,当tensor全是double型时,int函数会把所有元素取整,从1.5可以看出,不是四舍五入,而是取整.double函数又把整数型元素变成double型. th> a 0.0000 ...

  4. 3D数学基础学习之向量一

    向量-数学定义 对数学家而言,向量就是一个数字列表,对程序员而言则是另一种相似的概念,数组. 向量-几何定义 a.向量的大小就是向量的长度(模),向量的长度非负 b.向量的方向描述了空间中向量的指向. ...

  5. C++开源大全

    程序员要站在巨人的肩膀上,C++拥有丰富的开源库,这里包括:标准库.Web应用框架.人工智能.数据库.图片处理.机器学习.日志.代码分析等. 标准库 C++ Standard Library:是一系列 ...

  6. Oracle子查询(嵌套查询)

    概念: 所谓子查询,即一个select语句中嵌套了另外的一个或者多个select语句 需求:查找和Smith同部门的所有员工的id和last_name 目标: 员工id,last_name from: ...

  7. Log4Net 配置SQL2008数据库 并传入自定义业务对象

    最近根据业务需要,俺们老大要求我们了解一个c#的组件——Log4Net 这玩意儿从来没弄过,感觉挺深奥的,结果经过2天的研究,还算小有所成吧,基本思路已经清晰明了了,不过过程中遇到一些很奇葩的问题,和 ...

  8. Cheatsheet: 2015 12.01 ~ 12.31

    Mobile Setting Up the Development Environment iOS From Scratch With Swift: How to Test an iOS Applic ...

  9. KNN算法与Kd树

    最近邻法和k-近邻法 下面图片中只有三种豆,有三个豆是未知的种类,如何判定他们的种类? 提供一种思路,即:未知的豆离哪种豆最近就认为未知豆和该豆是同一种类.由此,我们引出最近邻算法的定义:为了判定未知 ...

  10. 学习mongo系列(三) update() save()

    一.update()方法 >db.user.update({"name":"user1"},{$set:{"title":" ...