*LeetCode-Convert Sorted List to Binary Search Tree
2015-09-28 23:03
204 查看
inorder就是顺序的一个linked list
每次找到list的中间 就是root 左边sub list中间就是左子 右边sub list的中间就是右子
但是注意只有一个node的情况 要把head设置成null 而且每次找到mid之后 要把mid之前的那个node.next = null
o(nlogn)
The method 1 constructs the tree from root to leaves. In this method, we construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Linked List, so that the tree can be constructed in
O(n) time complexity. We first count the number of nodes in the given Linked List. Let the count be n. After counting nodes, we take left n/2 nodes and recursively construct the left subtree. After left subtree is constructed, we allocate memory for root and
link the left subtree with root. Finally, we recursively construct the right subtree and link it with root.
While constructing the BST, we also keep moving the list head pointer to next so that we have the appropriate pointer in each recursive call.
从list头开始 用一个全局指针来当pointer 每次建好左树 建一个rootnode pointer ++ 然后建右树
每个node访问一遍 就是inorder的顺序 bottom up做的
public class Solution {
private ListNode node;
public TreeNode sortedListToBST(ListNode head) {
if ( head == null )
return null;
ListNode pt = head;
node = head;
int count = 0;
while ( head != null ){
head = head.next;
count ++;
}
return helper ( 0, count - 1 );
}
public TreeNode helper ( int start, int end ){
if ( start > end )
return null;
int mid = start + ( end - start ) / 2;
TreeNode left = helper ( start, mid - 1 );
TreeNode root = new TreeNode ( node.val );
root.left = left;
node = node.next;
root.right = helper ( mid + 1, end );
return root;
}
helper里面的start end只是用来计数的 我觉得 为了判断停止条件
每次找到list的中间 就是root 左边sub list中间就是左子 右边sub list的中间就是右子
但是注意只有一个node的情况 要把head设置成null 而且每次找到mid之后 要把mid之前的那个node.next = null
o(nlogn)
public class Solution { public TreeNode sortedListToBST(ListNode head) { if ( head == null ) return null; ListNode fast = head; ListNode slow = head; ListNode pre = null; while ( fast != null && fast.next != null ){ fast = fast.next.next; pre = slow; slow = slow.next; } if ( pre != null ) pre.next = null; else head = null; TreeNode root = new TreeNode ( slow.val ); root.left = sortedListToBST(head); root.right = sortedListToBST(slow.next); return root; } }o(n)做法
The method 1 constructs the tree from root to leaves. In this method, we construct from leaves to root. The idea is to insert nodes in BST in the same order as the appear in Linked List, so that the tree can be constructed in
O(n) time complexity. We first count the number of nodes in the given Linked List. Let the count be n. After counting nodes, we take left n/2 nodes and recursively construct the left subtree. After left subtree is constructed, we allocate memory for root and
link the left subtree with root. Finally, we recursively construct the right subtree and link it with root.
While constructing the BST, we also keep moving the list head pointer to next so that we have the appropriate pointer in each recursive call.
从list头开始 用一个全局指针来当pointer 每次建好左树 建一个rootnode pointer ++ 然后建右树
每个node访问一遍 就是inorder的顺序 bottom up做的
public class Solution {
private ListNode node;
public TreeNode sortedListToBST(ListNode head) {
if ( head == null )
return null;
ListNode pt = head;
node = head;
int count = 0;
while ( head != null ){
head = head.next;
count ++;
}
return helper ( 0, count - 1 );
}
public TreeNode helper ( int start, int end ){
if ( start > end )
return null;
int mid = start + ( end - start ) / 2;
TreeNode left = helper ( start, mid - 1 );
TreeNode root = new TreeNode ( node.val );
root.left = left;
node = node.next;
root.right = helper ( mid + 1, end );
return root;
}
helper里面的start end只是用来计数的 我觉得 为了判断停止条件
相关文章推荐
- 【css】day04_列表样式_显示方式_鼠标形状
- 5. 封装和继承
- Java中wait()与sleep()的区别
- xUtils基本介绍
- Android性能调优利器StrictMode
- 字符编码(一)
- LeetCode——N-Queens II
- sql注意事项
- 忘记mysql密码 重置密码
- memory prefetch浅析
- Hibernate 配置文件
- C. Developing Skills (CF Round #322 (Div.2) 贪心)
- 【线段树(单点修改,区间求和)】HDU1166 - 敌军布阵
- Github README.md 添加图片
- Github README.md 添加图片
- Github README.md 添加图片
- Block传值和传值之间的注意事项
- java中wait和sleep的区别
- 【UIKit-110-5】#import <UIKit/UITableView.h>选中行
- Quartz2D之生成圆形头像、打水印、截图三种方法的封装