【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height
题目:
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height.
Example
Given [1,2,3,4,5,6,7], return
4
/ \
2 6
/ \ / \
1 3 5 7
题解:
Solution 1 ()
class Solution {
public:
TreeNode *sortedArrayToBST(vector<int> &A) {
if (A.size() == ) return nullptr;
return buildTree(A, , A.size() - );
}
TreeNode* buildTree(vector<int>&A, int begin, int end) {
if (begin > end) {
return nullptr;
}
int mid = begin + (end - begin) / ;
TreeNode* root = new TreeNode(A[mid]);
root->left = buildTree(A, begin, mid - );
root->right = buildTree(A, mid + , end);
return root;
}
};
【Lintcode】177.Convert Sorted Array to Binary Search Tree With Minimal Height的更多相关文章
- 177. Convert Sorted Array to Binary Search Tree With Minimal Height【LintCode by java】
Description Given a sorted (increasing order) array, Convert it to create a binary tree with minimal ...
- Convert Sorted Array to Binary Search Tree With Minimal Height
Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Exa ...
- 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...
- 【easy】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】108. Convert Sorted Array to Binary Search Tree
Problem: Given an array where elements are sorted in ascending order, convert it to a height balance ...
- LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...
- 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)
[LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...
- 【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...
- 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告
原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...
随机推荐
- GIT简单使用——多人协作篇
多人协作的工作模式通常是这样:1.首先,可以试图用git push origin <branch-name>推送自己的修改:2.如果推送失败,则因为远程分支比你的本地更新,需要先用git ...
- 小明同学喜欢体育锻炼,他常常去操场上跑步。跑道是一个圆形,在本题中,我们认为跑道是一个半径为R的圆形,设圆心的坐标原点(0,0)。小明跑步的起点坐标为(R,0),他沿着圆形跑道跑步,而且一直沿着一个方向跑步。回到家后,他查看了自己的计步器,计步器显示他跑步的总路程为L。小明想知道自己结束跑步时的坐标,但是他忘记自己是沿着顺时针方向还是逆时针方向跑的了。他想知道在这两种情况下的答案分别是多少。
include "stdafx.h" #include<iostream> #include<vector> #include<string> ...
- POJ 1787 Charlie's Change
多重背包 可行性+路径记录 题意是说你要用很多其它的零钱去买咖啡.最后输出你分别要用的 1,5 ,10 .25 的钱的数量. 多重背包二进制分解.然后记录下 这个状态.最后逆向推就可以. #inclu ...
- C#中方法中 ref 和 out的使用
案例1: static void Main() { , , , }; int numLargerThan10,numLargerThan100,numLargerThan1000 ; Proc(ary ...
- Oracle exp使用正則表達式导出部分表
假设数据库中有许多张表,而我们又仅仅想导出须要的那几张能够使用例如以下命令 --仅仅导出test1.test2表 exp myname/mypassword@orcl file = d:\my.dmp ...
- Easy AR简单教程
Easy AR简单教程 相关SDK资源下载链接:http://pan.baidu.com/s/1dERtCWD 密码:o0jd 1.ImageTarget的制作 (1).导入EasyARSD包,删 ...
- Can a GridView have a footer and header just like ListView?
Aquick question: In ListView I use this code: list.addHeaderView(headerView); How to deal with it wh ...
- doT.js具体使用介绍
官网: http://olado.github.iodoT.js具体使用介绍 用法: {{= }} for interpolation {{ }} for evaluation {{~ }} for ...
- 九度OJ 1163:素数 (素数)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8496 解决:2887 题目描述: 输入一个整数n(2<=n<=10000),要求输出所有从1到这个整数之间(不包括1和这个整数 ...
- c++编码习惯
1 大驼峰命名法 类名和函数名由单词构成,每个单词的首字母大写. 2 函数命名 大驼峰命名法. 3 类命名 大驼峰命名,但是为了和函数名区分开,在前面加上一个大写的C.