convert-sorted-list-to-binary-search-tree
来源:互联网 发布:凯立德端口修改工具 编辑:程序博客网 时间:2024/06/03 19:10
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
class TreeNode(object): def __init__(self, val, left=None, right=None): self.data = val self.left = left self.right = rightclass Solution(object): def toBST(self, head, tail): if head != tail or head.next != tail: return head slow = head fast = head while fast is not None and fast.next is not None: slow = slow.next fast = fast.next.next root = TreeNode(head.data) root.left = self.toBST(head, slow) root.right = self.toBST(slow.next, fast) return root def sortedListToBST(self, head): return self.toBST(head, None)
阅读全文
0 0
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- convert sorted list to binary search tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Convert Sorted List to Binary Search Tree
- Spring Aop那些事
- 实验一 线性表的基本操作实现及其应用
- Android Studio添加aar
- Spring与Struts2整合
- AsyncTask
- convert-sorted-list-to-binary-search-tree
- tensorflow: 如何定义常量tensor与变量tensor
- Superset 数据分析平台搭建及使用 1
- 升级xCode9 导航栏变化及一些小技巧
- Android Button 英文大小写问题
- .git无法忽略target,或者不生效的情况
- Android 屏幕适配方案(七)
- Nodejs初识(一)
- Excel(XLS,XLSX)和CSV相互转换