【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 ...
随机推荐
- C++ Programming language读书笔记
C语言,结构化程序设计.自顶向下.逐步求精及模块化的程序设计方法;使用三种基本控制结构构造程序,任何程序都可由顺序.选择.循环三种基本控制结构构造. 模块结构:"独立功能,单出.入口&quo ...
- easyui dialog 扩展load
$.extend($.fn.panel.methods, { showMask: function(jq, msg){ return jq.each(function(){ var pal = $(t ...
- R----lubridata包介绍学习
lubridate包,非常强大,能够识别各种类型的日期.字符型和时间型数据,都是格式比较特别的你数据,在处理时,比较麻烦,但是有了lubridate这个包之后,时间处理变得非常简单,这个包函数命名简单 ...
- [问题2014A06] 复旦高等代数 I(14级)每周一题(第八教学周)
[问题2014A06] 若 \(n\) 阶实方阵 \(A\) 满足 \(AA'=I_n\), 则称为正交矩阵. 证明: 不存在 \(n\) 阶正交矩阵 \(A,B\) 满足 \(A^2=cAB+B^ ...
- oracle mysql sqlserver数据库中的分页
oracle: select * from (select rownum r,t1.* from tablename t1 where rownum <M+N ) t2 where t2.r&g ...
- gdb多进程调试
http://blog.csdn.net/nbabn/article/details/24984501 http://blog.csdn.net/zb872676223/article/details ...
- css模块化策略
为什么要模块化? 分治和复用 封装,不污染全局,不被全局污染. 继承 BEM(block:块,Element:元素,Modifier:修饰符)策略 .block__Element--Modifier ...
- C++用PostMessage模拟按钮点击
有时我们可能会在某个程序中用到模拟按钮点击事件. 本文中的例子在MFC程序中调试通过,duilib的没试过,还需探索 不多说,上代码: #include "stdafx.h" #i ...
- 快速排序,C语言实现
排序法里比较出名的,具体的算法看下图: 这篇博客说的通俗易懂:http://blog.csdn.net/morewindows/article/details/6684558 这是快速排序的基础,用代 ...
- 移动端rem处理字体的js代码
(function (doc, win) { var docEl = doc.documentElement, resizeEvt = 'orientationchange' in window ? ...