LeetCode OJ——Convert Sorted List to Binary Search Tree
http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
将一个按照元素升序排列的链表转换成BST。根据自身性质“元素升序排列”,再参考将升序排列的数组转换成BST的上一道题目,只需要将对数组的处理方式变成对链表的。还是得好好利用元素已经升序排列这个性质,不用那种纯粹的重新生成一个BST,按照左转、右转、先左转后右转、先右转后左转。
#include <iostream>
#include <vector> using namespace std; struct ListNode {
int val;
ListNode *next;
ListNode(int x) : val(x), next(NULL) {}
}; struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
}; class Solution {
public:
void fun(ListNode *begin ,int len,TreeNode *node)
{
if(len == )
{
node->val = begin->val;
return;
}
int leftlen = len/;
int rightlen = len - len/ -; ListNode *midNode = begin;
int tt = leftlen;
while(tt--)
midNode = midNode->next;
node->val = midNode->val; if(leftlen)
{
TreeNode *nodeLeft = new TreeNode();
node->left = nodeLeft;
fun(begin,leftlen,nodeLeft);
}
if(rightlen)
{
TreeNode *nodeRight = new TreeNode();
node->right = nodeRight;
fun(midNode->next,rightlen,nodeRight);
}
} TreeNode *sortedListToBST(ListNode *head) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(head == NULL)
return NULL; TreeNode *root = new TreeNode();
//length
ListNode *temp= head;
int len = ;
while(temp)
{
len++;
temp = temp->next;
}
fun(head,len,root); return root;
}
}; int main()
{
Solution *mySolution = new Solution(); ListNode *head = new ListNode();
ListNode *n1 = new ListNode();
head->next = n1;
ListNode *n2 = new ListNode();
n1->next = n2;
ListNode *n3 = new ListNode();
n2->next = n3;
ListNode *n4 = new ListNode();
n3->next = n4;
ListNode *n5 = new ListNode();
n4->next = n5;
mySolution->sortedArrayToBST(head); return ;
}
LeetCode OJ——Convert Sorted List to Binary Search Tree的更多相关文章
- LeetCode OJ——Convert Sorted Array to Binary Search Tree
http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 将一个升序的数组转换成 height balan ...
- [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 ...
- [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 ...
- [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 ...
- 【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#109 Convert Sorted List to Binary Search Tree
原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...
- leetcode -day19 Convert Sorted List to Binary Search Tree
1. Convert Sorted List to Binary Search Tree Given a singly linked list where elements are sorted ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
随机推荐
- Vmware安装与使用
1)双击击VMware Workstation启动安装 2)接下来Ctrl+N快捷键(或者点选菜单Flie->New virtual machine)来创建一个新的虚拟机. 3)Custom(a ...
- selenium2-元素管理方式及解析
1.管理文件格式:yaml 2.Yaml里面的内容格式: 3.格式说明: baidu_input后面接上":",直接回车,然后空两格 type与value这两个key是固定 ...
- Alpha版(内部测试版)发布
首先通过微信扫吗下载我们的软件校园服务,首先进去登录界面没账号点击注册,注册完就可以登录了,进去界面我们在二手交易这项功能里我们即可以事卖家又可以是买家如果我们卖东西点击商品出售,填写商品信息,商品图 ...
- BZOJ 3326: [Scoi2013]数数
数位DP,然而式子真的复杂 #include<cstdio> #include<algorithm> #include<cstring> using namespa ...
- 模拟 - BZOJ 1510 [POI2006] Kra-The Disks
BZOJ 1510 [POI2006] Kra-The Disks 描述 Johnny 在生日时收到了一件特殊的礼物,这件礼物由一个奇形怪状的管子和一些盘子组成. 这个管子是由许多不同直径的圆筒(直径 ...
- webdriver高级应用- 在HTML5的画布元素上进行绘画操作
#encoding=utf-8 import unittest from selenium import webdriver import time class TestDemo(unittest.T ...
- python-高级编程-02
[yield 详解 协同程序 生成器表达式] 1> yield def res (): for i in range(10): x = yield i r = res() print r.nex ...
- 第1章jquery选择器
一.jquery等价于$ jquery选择器继承了css选择器的风格. $("#ID")代替了document.getElementById()函数,即通过id获取元素. $(&q ...
- 第4章--变量,作用域和内存问题 jquery
4.1基本类型和引用类型的值 解析器要分析赋给变量的值是基本类型值还是引用类型的值 基本类型:undefined null boolean number string 引用类型的值: ...
- The 18th Zhejiang University Programming Contest Sponsored by TuSimple
Pretty Matrix Time Limit: 1 Second Memory Limit: 65536 KB DreamGrid's birthday is coming. As hi ...