题目:

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

代码:

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. /**
  10. * Definition for a binary tree node.
  11. * struct TreeNode {
  12. * int val;
  13. * TreeNode *left;
  14. * TreeNode *right;
  15. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  16. * };
  17. */
  18. class Solution {
  19. public:
  20. TreeNode* sortedListToBST(ListNode* head) {
  21. if ( !head ) return NULL;
  22. // p2 point to the pre node of mid
  23. ListNode dummy(-);
  24. dummy.next = head;
  25. ListNode *p1 = &dummy, *p2 = &dummy;
  26. while ( p1 && p1->next && p1->next->next ) { p1 = p1->next->next; p2 = p2->next;}
  27. // get the mid val & cut off the mid from linked list
  28. int val = p2->next->val;
  29. ListNode *h2 = p2->next ? p2->next->next : NULL;
  30. p2->next = NULL;
  31. // recursive process
  32. TreeNode *root = new TreeNode(val);
  33. root->left = Solution::sortedListToBST(dummy.next);
  34. root->right = Solution::sortedListToBST(h2);
  35. return root;
  36. }
  37. };

tips:

1. 沿用跟数组一样的思路,采用二分查找

2. 利用快慢指针和虚表头技巧;最终的目的是p2指向mid的前驱节点。

3. 生成该节点,并递归生成left和right (这里需要注意传递的是dummy.next而不是head,原因是如果链表中只有一个节点,传head就错误了)

============================================

第二次过这道题,熟练了一些。重点在于求ListNode的中间节点。

  1. /**
  2. * Definition for singly-linked list.
  3. * struct ListNode {
  4. * int val;
  5. * ListNode *next;
  6. * ListNode(int x) : val(x), next(NULL) {}
  7. * };
  8. */
  9. /**
  10. * Definition for a binary tree node.
  11. * struct TreeNode {
  12. * int val;
  13. * TreeNode *left;
  14. * TreeNode *right;
  15. * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  16. * };
  17. */
  18. class Solution {
  19. public:
  20. TreeNode* sortedListToBST(ListNode* head) {
  21. if (!head) return NULL;
  22. if (!head->next) return new TreeNode(head->val);
  23. ListNode* p1 = head;
  24. ListNode* pre = p1;
  25. ListNode* p2 = head;
  26. while ( p2 && p2->next )
  27. {
  28. pre = p1;
  29. p1 = p1->next;
  30. p2 = p2->next->next;
  31. }
  32. TreeNode* root = new TreeNode(p1->val);
  33. pre->next = NULL;
  34. root->left = Solution::sortedListToBST(head);
  35. root->right = Solution::sortedListToBST(p1->next);
  36. return root;
  37. }
  38. };

【Convert Sorted List to Binary Search Tree】cpp的更多相关文章

  1. 【Convert Sorted Array to Binary Search Tree】cpp

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

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

  3. 【二叉查找树】05根据升序的链表构造二叉查找树【Convert Sorted List to Binary Search Tree】

    利用递归,构造二叉查找树, ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给一个 ...

  4. 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】

    ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...

  5. 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  6. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  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. 面向初学者的 Windows 10 UWP 应用开发

    眼看 Windows 10 for Mobile 正式版也快要推送了,就先挖个坑吧,原文视频链接为:Windows 10 development for absolute beginners,以下博客 ...

  2. POJ C程序设计进阶 编程题#2:字符串中次数第2多的字母

    编程题#2:字符串中次数第2多的字母 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536k ...

  3. 一些peoplecode小技巧平【二】

    1. Set component changed page field property: For understanding this open a page in application desi ...

  4. 如何用asp.net MVC框架、highChart库从sql server数据库获取数据动态生成柱状图

    如何用asp.net MVC框架.highChart库从sql server数据库获取数据动态生成柱状图?效果大概是这样的,如图: 请问大侠这个这么实现呢?

  5. [leetcode]_Minimum Depth of Binary Tree

    第五道树题,10分钟之内一遍AC.做树题越来越有feel~ 题目:求一棵树从root结点到叶子结点的最短路径. 思路:仍然是递归.如果一个结点的左右子树任意一边为Null,该子树的minDepth应为 ...

  6. WPF: 读取XPS文件或将word、txt文件转化为XPS文件

    读取XPS格式文件或将doc,txt文件转化为XPS文件,效果图如下: 1.XAML页面代码: <Window x:Class="WpfWord.MainWindow" xm ...

  7. Maven的HTTP代理设置

    http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html   第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...

  8. luigi学习1

    一.luigi介绍 luigi是基于python语言的,可帮助建立复杂流式批处理任务管理系统.这些批处理作业典型的有hadoop job,数据库数据的导入与导出,或者是机器学习算法等等. luigi的 ...

  9. Hbase负载均衡流程以及源码

    hmater负责把region均匀到各个region server .hmaster中有一个线程任务是专门处理负责均衡的,默认每隔5分钟执行一次. 每次负载均衡操作可以分为两步: 生成负载均衡计划表 ...

  10. Ubuntu14.04安装GNOME3桌面

    以下是安装方法: sudo add-apt-repository ppa:gnome3-team/gnome3 sudo apt-get update sudo apt-get dist-upgrad ...