Comments (6)
// 将数组从中间分割,左边集合用来构建左子树,中间的点为根节点
var sortedListToBST = function(head) {
if (head == null || head.val == null) return null;
var arry = [];
while (head) {
if (head.val != null) {
arry.push(head.val);
}
head = head.next;
}
if (arry == null || arry.length == 0) return null;
return temp(0, arry.length - 1);
function temp(start, end) {
var node = new TreeNode();
if (end - start == 0) {
return new TreeNode(arry[end]);
}
if (end - start == 1) {
node.val = arry[end];
node.left = new TreeNode(arry[start]);
return node;
}
var middleIndex = parseInt((end - start) / 2) + start;
node.val = arry[middleIndex];
node.left = temp(start, middleIndex - 1);
node.right = temp(middleIndex + 1, end);
return node;
}
};
from leetcode.
python3
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def sortedListToBST(self, head: ListNode) -> TreeNode:
tmp = []
while head:
tmp.append(head.val)
head = head.next
def dfs(tmp):
if len(tmp) == 0:
return None
if len(tmp) == 1:
return TreeNode(tmp[0])
ii = int(len(tmp) / 2)
root = TreeNode(tmp[ii])
root.left = dfs(tmp[ : ii])
root.right = dfs(tmp[ii + 1 :])
return root
return dfs(tmp)
from leetcode.
golang
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func sortedListToBST(head *ListNode) *TreeNode {
if head == nil {
return nil
}
lenList := 0
cur := head
for cur != nil {
cur = cur.Next
lenList++
}
return binarySearch(&head, 0, lenList-1)
}
func binarySearch(head **ListNode, start int, end int) *TreeNode {
if start > end {
return nil //
}
mid := (start + end) / 2
left := binarySearch(head, start, mid-1)
root := &TreeNode{(*head).Val, nil, nil}
root.Left = left
*head = (*head).Next
//order must leftSubTree, head ,rightSubTree
right := binarySearch(head, mid+1, end)
root.Right = right
return root
}
复杂度分析
-
时间复杂度:
时间复杂度仍然为 O(N)
因为我们需要遍历链表中所有的顶点一次并构造相应的二叉搜索树节点。 -
空间复杂度:
O(logN)
from leetcode.
static TreeNode* bstFromSortedList(ListNode*& head, int start, int end) {
if (start > end) return nullptr;
auto mid = start + (end - start) / 2;
auto left = bstFromSortedList(head, start, mid - 1);
if (head == nullptr) return nullptr;
auto root = new TreeNode(head->val);
head = head->next;
root->left = left;
root->right = bstFromSortedList(head, mid + 1, end);
return root;
}
static TreeNode* bstFromSortedList(ListNode* head) {
auto len = 0;
auto p = head;
while (p != nullptr) {
++len;
p = p->next;
}
return bstFromSortedList(head, 1, len);
}
from leetcode.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from leetcode.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
from leetcode.
Related Issues (20)
- 算法学习 HOT 1
- 树专题中双色标记法后序和前序写反了 HOT 2
- leetcode/thinkings/tree.md 出错 HOT 1
- some error
- 二分查找专题,寻找最左/右插入位置算法模板错误问题 HOT 9
- possible code error in thinkings/heap.md HOT 1
- link error HOT 4
- link is not correct
- [695.最大岛屿面积,360,面试原题]【每日一题】 HOT 3
- 【专题】 反向思考 HOT 3
- 【专题】 考虑每一项对结果到的贡献
- 【专题】递推方程时间复杂度优化
- 已发布文章的代码错误 HOT 7
- Remove duplicate CPP solution and add Python solution for problem 100.same-tree
- Add OSSF Scorecard security workflow
- 题目的排版可否改一改
- 关于二分法中查找中间点索引的算式 HOT 6
- leetcode-thinkings-tree.md BFS 模版调整 HOT 3
- anki-card 中只有10道题吗?截止到2023.11 HOT 1
- 【每日一题】- 2020-xx-xx - xxx
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from leetcode.