leetcode dfs Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree

Total Accepted: 21420 Total
Submissions: 78476My Submissions

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

题意:把有序单链表转换为二叉查找树

思路:

用链表中心作为当作二叉树的根,

链表的前半段作为左子树、后半段作为右子树。

可用递归实现

复杂度:时间O(n* log n),空间O(log n)

TreeNode * dfs(ListNode *head, int size){
	if(size <= 0) return NULL;
	if(size == 1) return new TreeNode(head->val);

	ListNode *cur = head;
	for(int i = 0; i < size/2; ++i){
		cur = cur->next;
	}
	TreeNode *root = new TreeNode(cur->val);
	root->left = dfs(head, size/2);
	root->right = dfs(cur->next, size - size/2 - 1 );
	return root;
}

TreeNode *sortedListToBST(ListNode *head) {
	ListNode *cur = head;
	int size = 0;
	while(cur){
		++size;
		cur = cur->next;
	}
	return dfs(head, size);
}

时间: 2024-11-06 14:29:50

leetcode dfs Convert Sorted List to Binary Search Tree的相关文章

leetcode -day19 Convert Sorted List to Binary Search Tree

1.  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. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr

leetcode No109. 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. 把有序链表转化成平衡的BST Algorithm: 把链表转化成数组,再根据leetcode No108. Convert Sorted Array to Binary Search Tree的方法 找到数组中间的元素,作为根节点,则根节点左边是左子树,根节点

[Leetcode][BST][Convert Sorted Array to Binary Search Tree]

把一个排好序的vector转换为一颗二分查找树. 很简单的题目,递归即可,保证边界不要出错. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 1

[LeetCode 题解]: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. 题意:给定一个有序的链表,将其转换成平衡二叉搜索树 思路: 二分法 要构建一个平衡二叉树,二分法无疑是合适的,至于如何分是的代码简洁,就需要用到递归了. class Solution { public: // find middle element of the list Lis

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

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 代码:oj测试通过 Runtime: 178 ms 1 # Definition for a binary tree node 2 # class TreeNode: 3 # def __init__(self, x): 4 # self.val = x 5 # self

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh