由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 弱问:leetcode里Convert Sorted List to Binary Search Tree
相关主题
leetcode上的sorted list to BSThelp: leetcode "Recover Binary Search Tree" -- 附代码
java问题Find the node with given value in binary tree in in-order
明天电面,求建议leetcode Runtime error : Flatten Binary Tree to Linked List
java 链表里面dummy node 一问?谢谢刚刚电面bloomberg,被问到一个没看到过的问题
根据我面过的hp来的人,基本都没竞争力leetcode 上单链表转BST那道题求指导
Leetcode bst max path-----is this solution correct?弱问一个小问题,leetcode 上merge sorted list
check if a binary tree is a valid binary search tree这题怎么做?
[leetcode] Maximum Depth of Binary Treeleetcode上的Sort List那道题
相关话题的讨论汇总
话题: treenode话题: null话题: head2话题: list话题: head
进入JobHunting版参与讨论
1 (共1页)
s*****1
发帖数: 134
1
这题和上面一题(convert vector)是不是思路一样?
我就先把list转成vector做了
有没有专门给list的方法?
d*********g
发帖数: 154
2
你说的是这个么?http://www.leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html
感觉不是很好理解。
P*******U
发帖数: 203
3
不用转的,可以直接搞
public class Solution {
public TreeNode sortedListToBST(ListNode head) {
// Start typing your Java solution below
// DO NOT write main() function
if(head == null)
return null;

if(head.next == null)
return new TreeNode(head.val);

ListNode head1, head2;
head1 = head;
head2 = head;

while(head2!=null && head2.next!=null){
head1 = head1.next;
head2 = head2.next.next;
}

ListNode temp = head;

while(temp.next.val!=head1.val){
temp = temp.next;
}

temp.next = null;

TreeNode root = new TreeNode(head1.val);
root.left = sortedListToBST(head);
root.right = sortedListToBST(head1.next);

return root;

}
}
s*****1
发帖数: 134
4
hi, 谢谢2位楼上回复!
第一个的链接很有效,太感谢了!
第二个的算法很精妙,虽然复杂度可能不是linear,但是也给我提了个醒,谢谢啦
1 (共1页)
进入JobHunting版参与讨论
相关主题
leetcode上的Sort List那道题根据我面过的hp来的人,基本都没竞争力
[ 每日一课] Sort ListLeetcode bst max path-----is this solution correct?
[BSSD]回国一趟回来做题很难进入状态了,顺便问下那个Merge k Sortedcheck if a binary tree is a valid binary search tree
请大家 看看这个 Merge k Sorted Lists (Java), 我不太明白[leetcode] Maximum Depth of Binary Tree
leetcode上的sorted list to BSThelp: leetcode "Recover Binary Search Tree" -- 附代码
java问题Find the node with given value in binary tree in in-order
明天电面,求建议leetcode Runtime error : Flatten Binary Tree to Linked List
java 链表里面dummy node 一问?谢谢刚刚电面bloomberg,被问到一个没看到过的问题
相关话题的讨论汇总
话题: treenode话题: null话题: head2话题: list话题: head