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 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
随机推荐
- matplotlib绘图(四)
控制文字属性的方法: 所有的方法都会返回一个matplotlib.text.Text对象 文本注释: annnotate() xy参数设置箭头指示的位置,xytext参数设置注释文字的位置 arro ...
- Docker 自动运行Nginx容器
Dockerfile文件如下: FROM ubuntu #基础镜像 RUN apt-get update #更新apt RUN apt-get -y install nginx #安装nginx VO ...
- NodeJS基础API-path相关的问题basename,extname,dirname,parse,format,sep,delimiter,win32,posix
path 参考文档:http://nodejs.cn/api/path.html const {normalize} = require('path'); // ES6语法 // 相当于 const ...
- nginx访问日志(access_log)
一.nginx访问日志介绍 nginx软件会把每个用户访问网站的日志信息记录到指定的日志文件里,供网站提供者分析用户的浏览行为等,此功能由ngx_http_log_module模块负责,对应的官方地址 ...
- 【php】 phpword下载文件问题
这个问题是在 科锐国际 工作过程中发现的 word文档的名字(有汉字和空格)在windows系统上遍历是查不到文件的,但是在linux系统上市可以的压缩包里面的中文名word文档,如果出现汉字和空格, ...
- Django ORM (三) 查询,删除,更新操作
ORM 查询操作 修改 views.py 文件 from django.shortcuts import render, HttpResponse from app01 import models f ...
- JAVA获取网络图片并保存到本地(随机图片接口)
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import j ...
- debian使用sudo
debian默认没有sudo ,在命令前无法使用sudo #切换到根用户$ su 输入根用户密码 # apt-get install sudo # nano /etc/sudoers 文件的 User ...
- BZOJ 5215: [Lydsy2017省队十连测]商店购物
裸题 注意+特判 #include<cstdio> using namespace std; const int mod=1e9+7; int F[1000005],mi[10000005 ...
- 大数据学习——SparkStreaming整合Kafka完成网站点击流实时统计
1.安装并配置zk 2.安装并配置Kafka 3.启动zk 4.启动Kafka 5.创建topic [root@mini3 kafka]# bin/kafka-console-producer. -- ...