【LeetCode OJ】Convert Sorted List to Binary Search Tree
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:
- The node is the mid-node of the linked list.
- The node's left child is the list consisting of the list nodes before the mid-node.
- 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的更多相关文章
- 【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 ...
- 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. 把一 ...
- 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 ...
- 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 ...
- 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. 讲一 ...
- 【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 ...
- 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 ...
- 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 ...
随机推荐
- UA模拟
安卓QQ内置浏览器UA: Mozilla/5.0 (Linux; Android 5.0; SM-N9100 Build/LRX21V) AppleWebKit/537.36 (KHTML, like ...
- python协程与异步I/O
协程 首先要明确,线程和进程都是系统帮咱们开辟的,不管是thread还是process他内部都是调用的系统的API,而对于协程来说它和系统毫无关系; 协程不同于线程的是,线程是抢占式的调度,而协程是协 ...
- bootstrap-table 加载不了数据问题总结
1.Without server-side pagination data-side-pagination="client"(bs-table的设置) 服务器端代码: @Reque ...
- Intent意图
1.显式Intent button1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(Vie ...
- shell中 "" 跟 ''的区别
在bash里,这两个都是引号,用来表明字符串,区别是,双引号中的变量会被展开,而单引号中不再展开.举个例子:a="abc"echo "str=$a" # 结果显 ...
- BI Content、Metadata Repository
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...
- 学的一点点ps
从C语言的代码中解脱开始学ps,看到色彩鲜明的东西,心里只有那么爽.哈哈.只学习3天,虽然只是一些皮毛,可还是学到了一些以前不知道的东西.让我对ps多了很多兴趣,决定以后要自学更多的ps技能.要给图片 ...
- shockt通信
目前为止,我们使用的最多网络协议还是tcp/ip网络.通常来说,我们习惯上称为tcp/ip协议栈.至于协议栈分成几层,有两种说法.一种是五层,一种是七层. 5.应用层 4.传输层 3.网络 ...
- JSP 中EL表达式用法详解
EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user.sex} 所有EL都是以${ ...
- myeclipse/eclipse没有Project Facets的解决方法
http://www.cnblogs.com/jerome-rong/archive/2012/12/18/2822783.html 经常在eclipse中导入web项目时,出现转不了项目类型的问题, ...