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

分析: 

链表不同于数组的是需要先遍历一遍求出长度,然后再找到中点,其余思路与Convert Sorted Array to Binary Search Tree一致。

代码:

 /**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
private:
TreeNode* helper(ListNode* head, int sz) {
if (head == nullptr || sz <= ) {
return nullptr;
}
ListNode* runner = head;
for (int i = ; i < sz / ; ++i) {
runner = runner -> next;
}
TreeNode* root = new TreeNode(runner -> val);
root -> left = helper(head, sz / );
root -> right = helper(runner -> next, sz - sz / - );
return root;
}
public:
TreeNode* sortedListToBST(ListNode* head) {
int sz = ;
ListNode* runner = head;
while (runner != nullptr) {
sz++;
runner = runner -> next;
}
TreeNode* result = helper(head, sz);
return result;
}
};

LeetCode109 Convert Sorted List to Binary Search Tree的更多相关文章

  1. Leetcode109. Convert Sorted List to Binary Search Tree有序链表转换二叉搜索树

    给定一个单链表,其中的元素按升序排序,将其转换为高度平衡的二叉搜索树. 本题中,一个高度平衡二叉树是指一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过 1. 示例: 给定的有序链表: [-10 ...

  2. Leetcode: Convert sorted list to binary search tree (No. 109)

    Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...

  3. [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 bala ...

  4. [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树

    Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...

  5. 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 bala ...

  6. [Leetcode][JAVA] Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  7. 【leetcode】Convert Sorted List to Binary Search Tree

    Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted in as ...

  8. 【leetcode】Convert Sorted Array to Binary Search Tree

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  9. 【LeetCode OJ】Convert Sorted Array to Binary Search Tree

    Problem Link: http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Same idea ...

随机推荐

  1. Leetcode114. Flatten Binary Tree to Linked List二叉树展开为链表

    给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \ 3 4 6 将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 class Solution { publ ...

  2. Redis源码解析:18Hiredis同步API和回复解析API代码解析

    Redis的sentinel模式使用了Hiredis代码,Hiredis是redis数据库一个轻量级的C语言客户端库.它实现的向Redis发送命令的API函数redisCommand,使用方法类似于p ...

  3. tyvj 1423 GF和猫咪的玩具

    传送门 解题思路 题目比较水,floyd求出最短路取个最小值即可.结果joyoi时限写错了..好像只有0ms才能过??突然发现加了快读就T不加就A,数据在10000以下的还是scanf快啊. 代码 # ...

  4. POJ1151 离散化求矩形面积的并

    /*第一道离散化的题目,虽然是水题,不过还是很高兴...*/ #include<cstdio> #include<algorithm> #include<cstring& ...

  5. poj 1269 Intersecting Lines(判断两直线关系,并求交点坐标)

    Intersecting Lines Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12421   Accepted: 55 ...

  6. JAVA短信验证码 工具类

    MsgCodeUtil.java package com.hg.util; import com.soyea.enums.ResultEnum; import com.soyea.exception. ...

  7. 2019-10-18-WPF-解决-StylusPlugIn-点击穿透问题

    title author date CreateTime categories WPF 解决 StylusPlugIn 点击穿透问题 lindexi 2019-10-18 20:55:35 +0800 ...

  8. 【洛谷P1204】【USACO1.2】挤牛奶Milking Cows

    P1204 [USACO1.2]挤牛奶Milking Cows 题目描述 三个农民每天清晨5点起床,然后去牛棚给3头牛挤奶.第一个农民在300秒(从5点开始计时)给他的牛挤奶,一直到1000秒.第二个 ...

  9. Leetcode40. Combination Sum组合总数2 II

    给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合. candidates 中的每个数字在每个组合中只能使用一次. ...

  10. loadrunner11录制为空的解决办法(win7+chrome最新版本)

    参考:https://www.cnblogs.com/zhang-zhi/archive/2018/09/10/9622605.html loadrunner11在win7中,使用chrome浏览器的 ...