【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 ...
随机推荐
- 有向图的强连通图——Kosaraju
有向图的强连通分量: 相互可达关系,每一个集合都是有向图的一个强连通分量SCC. 把一个集合看成一个点,SCC就形成了一个有向无环图——DAG; 如果DFS选择不好,从A点开始DFS,就会 ...
- 为什么要在html和body加上“height:100%;”
元素中有内容的时候div才能被撑起来所以我给div加了背景但是也不显示,就是因为没有内容,这个时候的解决办法就是 html,body{ height:100%; }
- [问题2014A03] 复旦高等代数 I(14级)每周一题(第五教学周)
[问题2014A03] 设 \(A=(a_{ij})\) 为 \(n\,(n\geq 3)\) 阶方阵,\(A_{ij}\) 为第 \((i,j)\) 元素 \(a_{ij}\) 在 \(|A|\) ...
- 首师大附中科创教育平台 我的刷题记录 0325 50212228海岛帝国:LYF的太空运输站
今天给大家献上“D”级题:50212228海岛帝国:LYF的太空运输站!! 试题编号:0325 50212228海岛帝国:LYF的太空运输站 难度级别:D: 运行时间限制:40ms: 运行 ...
- linux应用程序开发-文件编程-系统调用方式
在看韦东山视频linux驱动方面有一些吃力,究其原因,虽然接触过linux应用程序编程,但是没有深入去理解,相关函数用法不清楚,正好看到国嵌视频对这一方面讲的比较透彻, 所以把学习过程记录下来,也作为 ...
- Evolutionary Computing: 2. Genetic Algorithm(1)
本篇博文讲述基因算法(Genetic Algorithm),基因算法是最著名的进化算法. 内容依然来自博主的听课记录和教授的PPT. Outline 简单基因算法 个体表达 变异 重组 选择重组还是变 ...
- 解决maven的报错
昨晚用Mars版本建maven工程,测试springboot,一路很顺畅,没有啥阻碍. 今天换了台机器,结果就不好用了,建完maven工程后,pom文件报错,该生成的代码结构也没有,更别提jar包了. ...
- Security » Authorization » 介绍
Introduction¶ 介绍 77 of 87 people found this helpful Authorization refers to the process that determi ...
- HTML中的target(_self,_blank)用法总结
HTML中的target(_self,_blank)用法总结 最近一个项目,多次遇到target='_self', target='_blank'的用法, 再次总结一下: 1.<a>标签 ...
- 根据屏幕的宽度使用不同的css-文件
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...