109. Convert Sorted List to Binary Search Tree
来源:互联网 发布:yy动态头像源码 编辑:程序博客网 时间:2024/06/05 00:15
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Example
21->2->3 => / \ 1 3
java
/** * Definition for ListNode. * public class ListNode { * int val; * ListNode next; * ListNode(int val) { * this.val = val; * this.next = null; * } * } * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */public class Solution { /* * @param head: The first node of linked list. * @return: a tree node */ private ListNode current; public TreeNode sortedListToBST(ListNode head) { // write your code here if (head == null) { return null; } current = head; int size = getNum(head); TreeNode node = toTree(size); return node; } private TreeNode toTree(int size) { if (size <= 0) { return null; } TreeNode left = toTree(size / 2); TreeNode root = new TreeNode(current.val); current = current.next; TreeNode right = toTree(size - 1 - size / 2); root.left = left; root.right = right; return root; } private int getNum(ListNode head) { int num = 0; while (head != null) { num++; head = head.next; } return num; } }
python
"""Definition of ListNodeclass ListNode(object): def __init__(self, val, next=None): self.val = val self.next = nextDefinition of TreeNode:class TreeNode: def __init__(self, val): self.val = val self.left, self.right = None, None"""class Solution: """ @param: head: The first node of linked list. @return: a tree node """ node = ListNode(0) def sortedListToBST(self, head): # write your code here if head == None: return None self.node = head size = self.getSize(head) return self.toTree(size) def getSize(self, head): num = 0 while head != None: head = head.next num += 1 return num def toTree(self, size): if size <= 0: return None left = self.toTree(size / 2) root = TreeNode(self.node.val) self.node = self.node.next right = self.toTree(size - 1 - size / 2) root.left = left root.right = right return root
阅读全文
0 0
- Tree-----109. 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
- 有限状态机设计
- 硅谷企业SaaS服务教父:如何衡量与优化SaaS企业的核心指标
- 静态库与动态库的创建与使用
- java日志文件log4j.properties配置详解
- 编程挑战:找一个字符串中最长的单词
- 109. Convert Sorted List to Binary Search Tree
- 页面中文传到后台再传到页面编码
- 机器学习技法课程学习笔记4 -- Soft-Margin Support Vector Machine
- log4j:WARN Please initialize the log4j system properly解决办法
- PG中支持两阶段提交协议
- 检测评价函数 intersection-over-union ( IOU )
- 世峰数字科技与维他奶有限公司达成三维工业动画项目合作-(sufencg.com)
- vue build :零配置快速开发
- eclipse安装activiti插件和基本使用