首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
有序链表变成二叉搜索树
[编程题]有序链表变成二叉搜索树
热度指数:20394
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32M,其他语言64M
算法知识视频讲解
给定一个单链表,其中的元素按升序排序,请将它转化成平衡二叉搜索树(BST)
示例1
输入
{-1,0,1,2}
输出
{1,0,2,-1}
说明:本题目包含复杂数据结构TreeNode、ListNode,
点此查看相关信息
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(114)
分享
提交结果有问题?
0个回答
2篇题解
添加回答
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
链表
树
难度:
0条回答
114收藏
19967浏览
热门推荐
通过挑战的用户
查看代码
牛客38441...
2022-09-14 17:54:50
=120191...
2022-09-14 15:27:13
牛客61312...
2022-09-11 16:11:06
牛客63727...
2022-09-02 11:12:37
痴心的芭乐在吃瓜
2022-08-28 20:28:43
相关试题
能正确表示逻辑式“a≥10或a≤0...
C++
C语言
评论
(1)
杨辉三角
递归
思维题
评论
(1)
自由落体
数学
NOIP复赛
评论
(2)
下列关于alpha、beta 测试...
软件测试
评论
(2)
下列哪些选项描述了Java中的抽象类?
Java
评论
(1)
有序链表变成二叉搜索树
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ /* * public class ListNode { * int val; * ListNode next = null; * } */ public class Solution { /** * * @param head ListNode类 * @return TreeNode类 */ public TreeNode sortedListToBST (ListNode head) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ /** * struct ListNode { * int val; * struct ListNode *next; * }; */ class Solution { public: /** * * @param head ListNode类 * @return TreeNode类 */ TreeNode* sortedListToBST(ListNode* head) { // write code here } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # # @param head ListNode类 # @return TreeNode类 # class Solution: def sortedListToBST(self , head ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /* * function ListNode(x){ * this.val = x; * this.next = null; * } */ /** * * @param head ListNode类 * @return TreeNode类 */ function sortedListToBST( head ) { // write code here } module.exports = { sortedListToBST : sortedListToBST };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # class ListNode: # def __init__(self, x): # self.val = x # self.next = None # # # @param head ListNode类 # @return TreeNode类 # class Solution: def sortedListToBST(self , head ): # write code here
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /* * type ListNode struct{ * Val int * Next *ListNode * } */ /** * * @param head ListNode类 * @return TreeNode类 */ func sortedListToBST( head *ListNode ) *TreeNode { // write code here }
{-1,0,1,2}
{1,0,2,-1}