【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.
代码:
/**
* 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* sortedArrayToBST(vector<int>& nums) {
const int len = nums.size();
return Solution::sortedArr2BST(nums, , len-);
}
static TreeNode* sortedArr2BST(vector<int>& nums, int begin, int end )
{
if ( begin>end ) return NULL;
int mid = (begin+end+)/;
TreeNode *root = new TreeNode(nums[mid]);
root->left = Solution::sortedArr2BST(nums, begin, mid-);
root->right = Solution::sortedArr2BST(nums, mid+, end);
return root;
}
};
tips:
利用二分查找+递归。
注意终止条件:begin>end,返回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* sortedArrayToBST(vector<int>& nums)
{
return Solution::transform(nums, , nums.size()-);
}
static TreeNode* transform(vector<int>& nums, int begin, int end)
{
if ( begin>end ) return NULL;
int mid = (begin+end)/;
TreeNode* root = new TreeNode(nums[mid]);
root->left = Solution::transform(nums, begin, mid-);
root->right = Solution::transform(nums, mid+, end);
return root;
}
};
【Convert Sorted Array to Binary Search Tree】cpp的更多相关文章
- 【二叉查找树】04根据升序数组构造二叉查找树【Convert Sorted Array to Binary Search Tree】
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个升序的数组,把他转换成一个 ...
- 【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 ...
- 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】
利用递归,构造二叉查找树, ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给一个 ...
- 【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 ...
- [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 ...
- 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-t ...
随机推荐
- POJ C程序设计进阶 编程题#4:括号匹配问题
编程题#4:扩号匹配问题 来源: POJ(Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩.) 注意: 总时间限制: 1000ms 内存限制: 65536kB 描述 在某 ...
- CentOS 5.x版本升级Mysql
#-----------------------------CentOS 5.x版本升级Mysql ------------------#! /bin/sh #1.关闭selinuxcp -rp /e ...
- C#中的委托、事件和设计模式(转载)
引言 委托和事件在 .Net Framework中的应用非常广泛,然而,较好地理解委托和事件对很多接触C#时间不长的人来说并不容易.它们就像是一道槛儿,过了这个槛的人,觉得真是太容易了,而没有过去的人 ...
- Retrieve Only First x Rows
From time to time you may have the need to grab the top X number of rows returned from a table. For ...
- 多次绑定DataGridView的DataSource会报错 NullReferenceExcepti
最近做了一个winform的项目,数据绑定在datagridview中,datagridview中的数据需要删除,分页,更新等之类的操作,所以就涉及到了datagridview的重新绑定问题,而且这些 ...
- 《第一行代码--Android》阅读笔记之数据持久化
1.升级数据库 为了避免手工清空数据(或卸载重装APP),重写SQLiteOpenHelper里面的onUpgrade()方法 引用自http://blog.csdn.net/longvslove ...
- Silverlight RadGridView的HeaderCellStyle样式
效果图 <UserControl x:Class="SilverlightApplication7.MainPage" xmlns="http://schemas. ...
- selenium+python cooking用法 (转)
selenium-webdriver(python)--cookie处理 driver.get_cookies() 获得cookie信息 add_cookie(cookie_dict) 向cooki ...
- AlertDialog.Builder对话框类的用法
1.在测试时,如何实现一个提示 可以使用 Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); //从资源文件str ...
- 1084. Broken Keyboard (20)
On a broken keyboard, some of the keys are worn out. So when you type some sentences, the characters ...