【Convert Sorted List to Binary Search Tree】cpp
题目:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
代码:
- /**
- * 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 {
- public:
- TreeNode* sortedListToBST(ListNode* head) {
- if ( !head ) return NULL;
- // p2 point to the pre node of mid
- ListNode dummy(-);
- dummy.next = head;
- ListNode *p1 = &dummy, *p2 = &dummy;
- while ( p1 && p1->next && p1->next->next ) { p1 = p1->next->next; p2 = p2->next;}
- // get the mid val & cut off the mid from linked list
- int val = p2->next->val;
- ListNode *h2 = p2->next ? p2->next->next : NULL;
- p2->next = NULL;
- // recursive process
- TreeNode *root = new TreeNode(val);
- root->left = Solution::sortedListToBST(dummy.next);
- root->right = Solution::sortedListToBST(h2);
- return root;
- }
- };
tips:
1. 沿用跟数组一样的思路,采用二分查找
2. 利用快慢指针和虚表头技巧;最终的目的是p2指向mid的前驱节点。
3. 生成该节点,并递归生成left和right (这里需要注意传递的是dummy.next而不是head,原因是如果链表中只有一个节点,传head就错误了)
============================================
第二次过这道题,熟练了一些。重点在于求ListNode的中间节点。
- /**
- * 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 {
- public:
- TreeNode* sortedListToBST(ListNode* head) {
- if (!head) return NULL;
- if (!head->next) return new TreeNode(head->val);
- ListNode* p1 = head;
- ListNode* pre = p1;
- ListNode* p2 = head;
- while ( p2 && p2->next )
- {
- pre = p1;
- p1 = p1->next;
- p2 = p2->next->next;
- }
- TreeNode* root = new TreeNode(p1->val);
- pre->next = NULL;
- root->left = Solution::sortedListToBST(head);
- root->right = Solution::sortedListToBST(p1->next);
- return root;
- }
- };
【Convert Sorted List to Binary Search Tree】cpp的更多相关文章
- 【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 ...
- 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 ...
- 【二叉查找树】05根据升序的链表构造二叉查找树【Convert Sorted List to Binary Search Tree】
利用递归,构造二叉查找树, ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给一个 ...
- 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...
- 【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【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 ...
- 【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 ...
- 【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 ...
随机推荐
- 面向初学者的 Windows 10 UWP 应用开发
眼看 Windows 10 for Mobile 正式版也快要推送了,就先挖个坑吧,原文视频链接为:Windows 10 development for absolute beginners,以下博客 ...
- POJ C程序设计进阶 编程题#2:字符串中次数第2多的字母
编程题#2:字符串中次数第2多的字母 来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536k ...
- 一些peoplecode小技巧平【二】
1. Set component changed page field property: For understanding this open a page in application desi ...
- 如何用asp.net MVC框架、highChart库从sql server数据库获取数据动态生成柱状图
如何用asp.net MVC框架.highChart库从sql server数据库获取数据动态生成柱状图?效果大概是这样的,如图: 请问大侠这个这么实现呢?
- [leetcode]_Minimum Depth of Binary Tree
第五道树题,10分钟之内一遍AC.做树题越来越有feel~ 题目:求一棵树从root结点到叶子结点的最短路径. 思路:仍然是递归.如果一个结点的左右子树任意一边为Null,该子树的minDepth应为 ...
- WPF: 读取XPS文件或将word、txt文件转化为XPS文件
读取XPS格式文件或将doc,txt文件转化为XPS文件,效果图如下: 1.XAML页面代码: <Window x:Class="WpfWord.MainWindow" xm ...
- Maven的HTTP代理设置
http://blog.sina.com.cn/s/blog_4f925fc30102ed3y.html 第一.检测本地网络是否不能直接访问Maven的远程仓库,命令为ping repo1.mav ...
- luigi学习1
一.luigi介绍 luigi是基于python语言的,可帮助建立复杂流式批处理任务管理系统.这些批处理作业典型的有hadoop job,数据库数据的导入与导出,或者是机器学习算法等等. luigi的 ...
- Hbase负载均衡流程以及源码
hmater负责把region均匀到各个region server .hmaster中有一个线程任务是专门处理负责均衡的,默认每隔5分钟执行一次. 每次负载均衡操作可以分为两步: 生成负载均衡计划表 ...
- Ubuntu14.04安装GNOME3桌面
以下是安装方法: sudo add-apt-repository ppa:gnome3-team/gnome3 sudo apt-get update sudo apt-get dist-upgrad ...