34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree
Convert Sorted List to Binary Search Tree
OJ: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/
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 binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
ListNode* findMiddleNode(ListNode *head, int length) {
if(length <= 0) return NULL;
int mid = (length+1) / 2;
for(int i = 1; i < mid; ++i) head = head->next;
return head;
}
TreeNode* createBST(ListNode *head, int length) {
ListNode *pMid = findMiddleNode(head, length);
TreeNode *root = NULL;
if(pMid) {
root = new TreeNode(pMid->val);
root->left = createBST(head, (length-1) >> 1);
root->right = createBST(pMid->next, length / 2);
}
return root;
}
int getLength(ListNode *head) {
int len = 0;
while(head) { len++; head = head->next; }
return len;
}
class Solution {
public:
TreeNode *sortedListToBST(ListNode *head) {
return createBST(head, getLength(head));
}
};
Convert Sorted Array to Binary Search Tree
OJ: https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
思想,同上。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
TreeNode* createBST(vector<int> &num, int l, int h) {
if(l > h) return NULL;
int mid = (l+h)/2;
TreeNode *root = new TreeNode(num[mid]);
root->left = createBST(num, l, mid-1);
root->right = createBST(num, mid+1, h);
return root;
}
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &num) {
return createBST(num, 0, num.size()-1);
}
};
34. Convert Sorted List to Binary Search Tree && Convert Sorted Array to Binary Search Tree的更多相关文章
- LeetCode108——Convert Sorted Array to Binary Search Tree
题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST ...
- [LeetCode] Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 这道 ...
- [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 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 ...
- LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree
LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...
- Convert Sorted Array to Binary Search Tree
Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...
- 37. leetcode 108. Convert Sorted Array to Binary Search Tree
108. Convert Sorted Array to Binary Search Tree 思路:利用一个有序数组构建一个平衡二叉排序树.直接递归构建,取中间的元素为根节点,然后分别构建左子树和右 ...
- [LeetCode] 108. Convert Sorted Array to Binary Search Tree ☆(升序数组转换成一个平衡二叉树)
108. Convert Sorted Array to Binary Search Tree 描述 Given an array where elements are sorted in ascen ...
随机推荐
- hdu 4642 Fliping game
http://acm.hdu.edu.cn/showproblem.php?pid=4642 对于给定的矩阵 操作步数的奇偶性是确定的 奇数步Alice赢 否则Bob赢 从左上角向右下角遍历遇到1就进 ...
- C语言基础--变量存储细节
1.变量为什么要有类型? 每种类型占用的内存空间不一样 int 4, char 1 double 8 2.只要定义变量, 系统就会开辟一块存储空间给我们的变量存储数据, 内存寻址是从大到小 3.越 ...
- 安装EditPlus
生成注册码:http://www.jb51.net/tools/editplus/ 862C2-DABE8-E30AA-CCE33-65E69
- java string.format()
String text=String.format("$%1$s 门市价:¥%2$s",18.6,22);$18.6 门市价:¥22
- Java EE注册三部曲(一步曲)
一步曲(html+servlet+SQL+Bean+Dao+加密(Base64)) 设计思路: 1:编写前台页面jsp:register.jsp,使得用户能够实行注册操作 2:编写servlet:re ...
- C++虚函数示例
和Java不同,CDerive derive语句可以直接生成对象,不需要new关键字 重载虚函数才可以用父类引用调用子类对象,重载普通函数没有效果 #include<iostream> # ...
- [Java Basics2] Iterable, Socket, Reflection, Proxy, Factory DP
Parent interface of Collection: Iterable Interface A class that implements the Iterable can be used ...
- 福州月赛2057 DFS
题意:告诉你族谱,然后Q条查询s和t的关系,妈妈输出M,爸爸输出F: 题目地址:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=78233# ...
- windows python3.2 shell环境(python叫做解释器)
[进入python的shell 环境:](python里称作命令解释器,windows叫做cmd,unix叫做shell) cmd 输入set path=%path%;e:\python2.7然后输 ...
- Linux TC流量控制HOWTO中文版
<本文摘自Linux的高级路由和流量控制HOWTO中文版 第9章节>网人郭工进行再次编译: 利用队列,我们可以控制数据发送的方式.记住我们只能对发送数据进行控制(或称为整形).其实,我们无 ...