问题

给出一个元素以递增序列排序的单链表,将其转换为一棵高度平衡的二叉搜索树。

初始思路

二叉搜索树高度平衡,意味着左右子树的高度要平衡。根据二叉树左子树节点小于根节点,右子树节点大于根节点的性质:我们在待选节点中选择值为中位数的节点作为根节点,所有小于中位数的节点作为左子树,所有大于中位数的节点作为右子树,即可满足高度平衡的要求。由于题目给出的已经是排好序的单链表,我们只要每次选择中间的节点即可。然后通过递归处理左右子树,最终完成高度平衡二叉搜索树的构建。

最终完成代码如下:

 class Solution {
enum ChildType
{
LEFT,
RIGHT
};
public:
TreeNode *sortedListToBST(ListNode *head)
{
if(!head)
{
return nullptr;
} ListNode* middle = FindMiddlePoint(head, nullptr); TreeNode* root = new TreeNode(middle->val); MakeSubTree(head, middle, root, LEFT);
MakeSubTree(middle->next, nullptr, root, RIGHT); return root;
} void MakeSubTree(ListNode* head, ListNode* end, TreeNode* parent, ChildType childType)
{
if(head == end)
{
return;
} ListNode* middle = FindMiddlePoint(head, end); TreeNode* treeNode = new TreeNode(middle->val); if(childType == LEFT)
{
parent->left = treeNode;
}
else
{
parent->right = treeNode;
} MakeSubTree(head, middle, treeNode, LEFT);
MakeSubTree(middle->next, end, treeNode, RIGHT);
} ListNode* FindMiddlePoint(ListNode* head, ListNode* end)
{
int length = ;
ListNode* node = head; while(node != end)
{
++length;
node = node->next;
} length /= ; node = head;
for(int i = ; i < length; ++i)
{
node = node->next;
} return node;
}
};

sortedListToBST

Judge Small和Judge Large皆顺利通过。

[LeetCode 109] - 将已排序链表转换为二叉搜索树 (Convert Sorted List to Binary Search Tree)的更多相关文章

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

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

  2. [Swift]LeetCode109. 有序链表转换二叉搜索树 | 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 ...

  3. [Swift]LeetCode108. 将有序数组转换为二叉搜索树 | Convert Sorted Array to Binary Search Tree

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

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

    [题目] 给出的升序排序的数组,个数必为奇数,要求形成二叉搜索(平衡)树. [思路] 辅助函数fun,[0,len]=>[0,mid-1]+[mid+1,len]. 当left>right ...

  5. LeetCode(109):有序链表转换二叉搜索树

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

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

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

  8. Convert Sorted List to Binary Search Tree——将链表转换为平衡二叉搜索树 &&convert-sorted-array-to-binary-search-tree——将数列转换为bst

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

  9. [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. Fo ...

随机推荐

  1. IBinder对象在进程间传递的形式(一)

    命题 当service经常被远程调用时,我们经常常使用到aidl来定一个接口供service和client来使用,这个事实上就是使用Binder机制的IPC通信.当client bind servic ...

  2. 二分-hdu-4768-Flyer

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4768 题目意思: 有n个A.B.C,每个Ai,Bi,Ci,对于每个P=Ai+k*Ci(P<=B ...

  3. [Javascript] Advanced Function Scope

    Something like 'for' or 'while', 'if', they don't create a new scope: ,,]; ; i < ary.length; i++) ...

  4. qDebug 学习小结

    在qtcentre中看到有网友问这样一个问题: Why this doesn't work? qDebug() << "Test" << std::endl ...

  5. Ubuntu12.04下使用valgrind内存测试工具测试Qt程序

    1. 到官网http://valgrind.org/downloads/上下载valgrind最新版本: 2. 解压源码,执行./configure;make;make install后,默认安装到/ ...

  6. Could not reliably determine the server's fully qualified domain name

    启动apache报错: [root@namenode1 ]# service httpd start Starting httpd: httpd: Could not reliably determi ...

  7. java07循环结构

    public class WhileTest { // while循环结构 public static void main(String[] args) { System.out.println(&q ...

  8. SIEBEL安装问题

    安装siebel 分三步走: 1.安装oracle 11g 2.安装Client 3.分别安装siebel tools.siebel web client,之后打上补丁 安装siebel tools. ...

  9. java语言基础特性

    使用Java5中的类型安全枚举和注解(以及元注解)编程 http://www.importnew.com/11053.html

  10. (转)关于c#中的事件

    原文链接http://blog.csdn.net/joyhen/article/details/8500211 如有不明白的地方欢迎加QQ群14670545 探讨 最近在看委托,然后看到事件,以前一直 ...