109. Convert Sorted List to Binary Search Tree

有点没有完全想明白。

能够理解的点有:

1. 给你一个sorted list,用这里面的树构建bst,说明这个list是该bst的inorder遍历。

2. 给你的list相当于一个queue,每次用掉一个node就往后移动一格,相当于queue.poll();

3. 和之前那题serialize的题有点像,我当时选择的方式是preorder,这里是Inorder。但是那个里面叶节点会用#标记,这里没有#, 只能用一个start, end来标记,如果start>end那么就说明已经到叶节点了,返回空,所以建树的顺序是 left = 递归()=> 创建root => root.left = left => root.right = 递归另一半

 1 private ListNode node;
 2     public TreeNode sortedListToBST(ListNode head) {
 3         if(head == null) {
 4             return null;
 5         }
 6         int size = 0;
 7         node = head;
 8         ListNode cur = head;
 9         while(cur != null) {
10             cur = cur.next;
11             size++;
12         }
13         return constructTree(0, size - 1);
14     }
15
16     private TreeNode constructTree(int start, int end) {
17         if(start > end) {
18             return null;
19         }
20         int mid = start + (end - start) / 2;
21         TreeNode left = constructTree(start, mid - 1);
22         TreeNode root = new TreeNode(node.val);
23         root.left = left;
24         node = node.next;
25         root.right = constructTree(mid + 1, end);
26         return root;
27     }

我觉得时间复杂度是O(n),因为node只走了一遍。

可以把serialize和deserialize用inorder再做一遍

时间: 2024-10-06 20:31:37

109. Convert Sorted List to Binary Search Tree的相关文章

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

109.Convert Sorted List to Binary Search Tree Leetcode Python

Convert Sorted List to Binary Search Tree Total Accepted: 32343 Total Submissions: 117376 My Submissions Question Solution Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST 这题和convert array t

【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 本题大意:给定一个单向链表,构造出平衡二叉搜索树 解题思路:参考[一天一道Leet

[leedcode 109] Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ /** * D

【LeetCode】109& - Convert Sorted List to Binary Search Tree&Convert Sorted Array to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Solution 1:  recursion runtime: 28ms. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i

LeeCode 109.Convert Sorted List to Binary Search Tree(将排序链表转化为BST) 解题思路和方法

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:此题与排序数组很大的不同是链表不知道长度以及上面的值.其整体思路还是中间值作为根节点,但是需要一点策略,不然就会超时. 先整体遍历长度之后,将长度保存,这样就不需要每次都遍历链表了. 代码如下: /** * Definition for singly-linked list

【Leetcode】109. Convert Sorted List to Binary Search Tree

Question: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node n

Java for LeetCode 109 Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 同上题,JAVA实现如下: public TreeNode sortedListToBST(ListNode head) { ArrayList<Integer> list=new ArrayList<Integer>(); while(head!=nu

109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; struct TreeNode { int val; TreeNode *left; TreeNode *r