109. Convert Sorted List to Binary Search Tree
来源:互联网 发布:sql执行顺序 编辑:程序博客网 时间:2024/05/18 22:44
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
从上往下递归构造二叉树,这样每次都要遍历时间复杂度高一些
1ms
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *//** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode sortedListToBST(ListNode head) { if(head==null) return null; ListNode tail=null; return toBST(head,tail); } public TreeNode toBST(ListNode head,ListNode tail){ ListNode fast=head; ListNode slow=head; if(head==tail) return null; while(fast!=tail&&fast.next!=tail){ fast=fast.next.next; slow=slow.next; } TreeNode newhead=new TreeNode(slow.val); newhead.left=toBST(head,slow); newhead.right=toBST(slow.next,tail); return newhead; }}
为什么这个更慢呢,。。
使用108题处理数组的方法,先把列表的值存到一个list里 然后递归不断调用
3ms
public class Solution { public TreeNode sortedListToBST(ListNode head) { if(head==null) return null; List<ListNode> list=new ArrayList<ListNode>(); while(head!=null){ list.add(head); head=head.next; } return sort(list,0,list.size()-1); } public TreeNode sort( List<ListNode> list,int start,int end){ if(start>end) return null; int mid=start+(end-start)/2; TreeNode root=new TreeNode(list.get(mid).val); root.left=sort(list,start,mid-1); root.right=sort(list,mid+1,end); return root ; }}
使用了中序遍历的顺序来构建这棵树。不用每次都遍历链表。而且不用另开空间
是在superchao在discuss中记录下来的,用java改写了一下第一次提交竟然说error。。。过了一会提交就过了 最近这个leetcode改了之后时常出现这种问题 尴尬
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } *//** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { ListNode list; public TreeNode sortedListToBST(ListNode head) { this.list=head; return sort(count(head)); } public int count(ListNode head){ int size=0; while(head!=null){ size++; head=head.next; } return size; } public TreeNode sort(int n){ if(n==0) return null; TreeNode node=new TreeNode(0); node.left=sort(n/2); //节点个数 node.val=list.val; //前n/2是左子树的节点,这个节点刚好是 根节点 list=list.next; node.right=sort(n-n/2-1); //这是右子树的节点 return node; }}
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
- BFS+DFS---简介与模板
- 说下入职IT小公司的感想
- *[Lintcode]Perfect Square
- 12-内部类
- tomcat远程debug
- 109. Convert Sorted List to Binary Search Tree
- 线程的生死
- 我为什么选择了sublime text
- TCP/IP illustrated 阅读笔记(六) ICMP协议
- hdoj2037 贪心算法——今年暑假不AC
- read代码阅读一(linux3.10.14)
- java虚拟机内存区域
- Javaweb-1 server.xml和web.xml中的配置
- 通过GC日志分析程序性能与内存泄漏