【本文链接】

http://www.cnblogs.com/hellogiser/p/array-to-binary-search-tree.html

题目

编写一个程序,把一个有序整数数组放到二叉搜索树中。

例如 4,6,8,10,12,14,16

转换为二叉搜索树为

10

/ \

6    14

/ \    /  \

4   8 12   16

【分析】

按照中序遍历的方法转换到二叉搜索树中。先要确定一个根节点,然后递归创建左右子树。

代码

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 
/*
    version: 1.0
    author: hellogiser
    blog: http://www.cnblogs.com/hellogiser
    date: 2014/5/31
*/
// binary tree node struct
struct BinaryTreeNode
{
    int value;
    BinaryTreeNode *left;
    BinaryTreeNode *right;
};

// array to tree recursively
BinaryTreeNode *arrayToTree(int array[], int start, int end)
{
    if (start > end)
        return NULL;
    ;
    BinaryTreeNode *root = new BinaryTreeNode();
    root->value = array[mid];
    root->left = arrayToTree(array, start, mid - );
    root->right = arrayToTree(array, mid + , end);
    return root;
}

// array to tree
BinaryTreeNode *ArrayToTree(int array[], unsigned int n)
{
    if (NULL == array || n<=0)
        return NULL;
    );
}

【参考】

http://blog.csdn.net/dahai_881222/article/details/7816127

【本文链接】

http://www.cnblogs.com/hellogiser/p/array-to-binary-search-tree.html

66. 有序数组构造二叉搜索树[array to binary search tree]的更多相关文章

  1. 【遍历二叉树】07恢复二叉搜索树【Recover Binary Search Tree】

    开一个指针数组,中序遍历这个二叉搜索树,将节点的指针依次保存在数组里, 然后寻找两处逆序的位置, 中序便利里BST得到的是升序序列 ++++++++++++++++++++++++++++++++++ ...

  2. 算法:非平衡二叉搜索树(UnBalanced Binary Search Tree)

    背景 很多场景下都需要将元素存储到已排序的集合中.用数组来存储,搜索效率非常高: O(log n),但是插入效率比较低:O(n).用链表来存储,插入效率和搜索效率都比较低:O(n).如何能提供插入和搜 ...

  3. [Swift]LeetCode669. 修剪二叉搜索树 | Trim a Binary Search Tree

    Given a binary search tree and the lowest and highest boundaries as L and R, trim the tree so that a ...

  4. LeetCode 669. 修剪二叉搜索树(Trim a Binary Search Tree)

    669. 修剪二叉搜索树 669. Trim a Binary Search Tree 题目描述 LeetCode LeetCode669. Trim a Binary Search Tree简单 J ...

  5. LeetCode 98. 验证二叉搜索树(Validate Binary Search Tree)

    题目描述 给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征: 节点的左子树只包含小于当前节点的数. 节点的右子树只包含大于当前节点的数. 所有左子树和右子树自身必须也 ...

  6. 《数据结构与算法分析——C语言描述》ADT实现(NO.03) : 二叉搜索树/二叉查找树(Binary Search Tree)

    二叉搜索树(Binary Search Tree),又名二叉查找树.二叉排序树,是一种简单的二叉树.它的特点是每一个结点的左(右)子树各结点的元素一定小于(大于)该结点的元素.将该树用于查找时,由于二 ...

  7. [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. 这道 ...

  8. LeetCode 108. 将有序数组转换为二叉搜索树(Convert Sorted Array to Binary Search Tree) 14

    108. 将有序数组转换为二叉搜索树 108. Convert Sorted Array to Binary Search Tree 题目描述 将一个按照升序排列的有序数组,转换为一棵高度平衡二叉搜索 ...

  9. [leetcode-108,109] 将有序数组转换为二叉搜索树

    109. 有序链表转换二叉搜索树 Given a singly linked list where elements are sorted in ascending order, convert it ...

随机推荐

  1. The constructor ClassPathXmlApplicationContext(String) refers to the missing type BeansException

    Spring4.X配置一个简单的读取sping的xml的配置文件结果

  2. 【poj3020】 Antenna Placement

    http://poj.org/problem?id=3020 (题目链接) 题意 给出一个矩阵,矩阵中只有‘*’和‘o’两种字符,每个‘*’可以向它上下左右四个方位上同为‘*’的点连一条边,求最少需要 ...

  3. codevs1500 后缀排序

    题目描述 Description 天凯是MIT的新生.Prof. HandsomeG给了他一个长度为n的由小写字母构成的字符串,要求他把该字符串的n个后缀(suffix)从小到大排序. 何谓后缀?假设 ...

  4. 从ICLassFactory 为 CLSID的COM组建创建实例失败:c001f011

    在sqlserver创建计划任务的时候,保存时出现:“从ICLassFactory 为 CLSID的COM组建创建实例失败:c001f011”. 解决方法:在运行sqlserver时,使用“以管理员身 ...

  5. C/C++代码中的笔误

    1. 在printf()的参数前加& (2015/10/7) 这是我写的一个数据生成器(generator)片段 +; printf("%d\n", &n);

  6. appium跑demo简单实例讲解

    安装appium,设置 demo.pyfrom appium import webdriver #要装webdriver,方法查看http://www.cnblogs.com/sincoolvip/p ...

  7. PHP实现发红包程序(helloweba网站经典小案例)

    我们先来分析下规律. 设定总金额为10元,有N个人随机领取: N=1 第一个 则红包金额=X元: N=2 第二个 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数. 第 ...

  8. 初学hibernate之缓存

    一.1.Session级别缓存属于一级缓存,持久化对象保存在Session一级缓存中(一级缓存引用持久化对象地址),只要session不关闭,一级缓存就存在,缓存中对象也不会被回收: Session会 ...

  9. php多态设计

    原文:http://www.cnblogs.com/tecs27/archive/2012/03/13/2394028.html 多态性是指相同的操作或函数.过程可作用于多种类型的对象上并获得不同的结 ...

  10. UICollectionViewController用法

    在iOS 6 发布前,开发人员习惯使用UITableView来展示几乎所有类型的数据集合.ios 6 为 IOS 引入了全新的控制器,用来显示数据集合,集合视图控制器是与表视图控制器类似的全新UI框架 ...