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 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
随机推荐
- Java AES加密解密工具 -- GUI 、在线传输文件
原理 对于任意长度的明文,AES首先对其进行分组,每组的长度为128位.分组之后将分别对每个128位的明文分组进行加密. 对于每个128位长度的明文分组的加密过程如下: (1)将128位AES ...
- 09GNU C语言程序编译
1. C 语言程序概述 GNU gcc 对 ISO 标准 C89 描述的 C 语言进行了一些扩展,其中一些扩展部分已经包括进 IOS C99 标准中.本节给出了内核中经常用到的一些 gcc 扩展语 ...
- HMAC(Hash-based Message Authentication Code)实现原理
1.HMAC 概念 HMAC(Hash-based Message Authentication Code)基于 hash 的消息验证码,是 安全通信中必要的组成部件. 主要是 防止消息被篡改,和对称 ...
- hdu 6333
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- Python虚拟机框架
Python字节码 我们知道,Python源代码在执行前,会先将源代码编译为字节码序列,Python虚拟机就根据这些字节码进行一系列的操作,从而完成对Python程序的执行.在Python2.5中,一 ...
- 文件上传下载,命令之wget / curl / which / sort / uniq / cut / wc /tr /sed
目录 命令 1.文件的上传下载 2.从外网下载文件wget 3.curl文件下载 4.查找命令which 5.字符处理命令-排序sort 6.字符处理-去重uniq 7.字符处理-截取cut 8.字符 ...
- Notepad++ WebEdit插件
虽然PHP的IDE有很多,但是,我还是比较喜欢用Notepad++这款编辑器,因为比较轻量级,而且用起来比较顺手. 但是最近在改别人写的代码的时候,经常要在选定的php前后插入<?php ?& ...
- Leetcode28--->字符串的匹配(KMP)
题目: 题目的本质是给定两个字符串str1,str2,求str1中的str2串开始的地方,即字符串的匹配,KMP算法 思路:时间复杂度为O(m + n),空间复杂度为O(n),原串的长度为m,子串的长 ...
- dev c++ 提示没有iostream.h文件
dev c++ 提示没有iostream.h文件 解决办法路径没有打通最好是这样写:#include <iostream>using namespace std;int main(int ...
- 九度oj 1003
前几天开刷九度oj,准备把做的题都放上,先放1003 题目1003:A+B 时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:16923 解决:7029 题目描述: 给 ...