题目:

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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 【LeetCode】108. Convert Sorted Array to Binary Search Tree 解题报告 (Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Java解法 Python解法 日期 题目地址:ht ...

  4. 【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 ...

  5. 【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 ...

  6. LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

    C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; ...

  7. 【LeetCode】109. Convert Sorted List to Binary Search Tree 解题报告(Python)

    [LeetCode]109. Convert Sorted List to Binary Search Tree 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  8. 【一天一道LeetCode】#108. Convert Sorted Array to Binary Search Tree

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  9. 【原创】leetCodeOj ---Convert Sorted List to Binary Search Tree 解题报告

    原题地址: https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题目内容: Given a sing ...

随机推荐

  1. 一个数组nums,其中任意两个值等于给定值target,返回这两个值在nums里的位置

    package com.java.tencent; import java.lang.reflect.Array; import java.util.Arrays; import java.util. ...

  2. 从epoll构建muduo-12 多线程入场

    mini-muduo版本号传送门 version 0.00 从epoll构建muduo-1 mini-muduo介绍 version 0.01 从epoll构建muduo-2 最简单的epoll ve ...

  3. 数据库ACID操作---事务四原则

    事务操作四原则: 1>原子性:简单来说——整个事务操作如同原子已经是物理上最小的单位,不可分离事务操作要么一起成功,要么一起失败. 2>一致性:倘若事务操作失败,则回滚事务时,与原始状态一 ...

  4. 06 php 单例模式

    一:单例模式的三大原则 (1)构造函数需要标记为非public(防止外部使用new操作符创建对象),单例类不能在其他类中实例化,只能被自身实例化. (2)拥有一个保存类的实例的静态成员变量$_inst ...

  5. Linux系统初始化流程

    POST-->BIOS(Boot Sequence)-->MBR(bootloader)-->Kernel(initrd)-->/sbin/init(/etc/inittab) ...

  6. MySQL 优化1

    系统在应用时间很长的情况下会慢慢变得很慢,无论是人还是机器为了更好的工作和学习都需要适当学习.数据库也是一样的用久了, 自然就会产生空间碎片,需要我们都i数据库中的数据块进行维护和整理.下面以实例来说 ...

  7. jQuery学习笔记(7)--表格展开关闭

    <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> &l ...

  8. Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fromHere.com' is therefore not allowed access.

    Failed to load http://wantTOgo.com/get_sts_token/: No 'Access-Control-Allow-Origin' header is presen ...

  9. When Programmers and Testers Collaborate

    When Programmers and Testers Collaborate Janet Gregory SOMETHING MAGICAL HAPPENS when testers and pr ...

  10. ABAP HTTP POST

    1.HTTP DATA: lo_http_client TYPE REF TO if_http_client, lv_service TYPE string, lv_result TYPE strin ...