【本文链接】

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. ASP.NET加JS方式

    一.如果是asp.net中的控件有OnClientClick事件,可以在控件中直接加 OnClientClick--客户端点击事件 二.如果asp.net中的控件没有OnClientClick事件,可 ...

  2. sql-数据库的隔离级别

    read uncommited  (读未提交)      最低级别,可读取未提交事物的数据,这会导致脏读,比如:某时刻会话a修改了一个数据,但还未提交,此时会话b,读取了该数据,这是,会话a回滚了事物 ...

  3. 最大ASCII的和问题

    问题:One day when you are going to clear all your browsing history, you come up with an idea: You want ...

  4. codevs3031 最富有的人

    题目描述 Description 在你的面前有n堆金子,你只能取走其中的两堆,且总价值为这两堆金子的xor值,你想成为最富有的人,你就要有所选择. 输入描述 Input Description 第一行 ...

  5. BZOJ3172 后缀数组

    题意:求出一篇文章中每个单词的出现次数 对样例的解释: 原文是这样的: a aa aaa 注意每个单词后都会换行 所以a出现次数为6,aa为3 (aa中一次,aaa中两次),aaa为1 标准解法好像是 ...

  6. 高效图片轮播,两个imageView实现

    本文是投稿文章,作者:codingZero 导语 在不少项目中,都会有图片轮播这个功能,现在网上关于图片轮播的框架层出不穷,千奇百怪,笔者根据自己的思路,用两个imageView也实现了图片轮播,这里 ...

  7. 【转】KMP算法

    转载请注明来源,并包含相关链接.http://www.cnblogs.com/yjiyjige/p/3263858.html 网上有很多讲解KMP算法的博客,我就不浪费时间再写一份了.直接推荐一个当初 ...

  8. spark.SecurityManager: SecurityManager: authentication disabled

  9. JS 下拉菜单

    HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...

  10. 行为Behavior的使用

    原文地址:http://www.it610.com/article/4918541.htm 行为就是继承yii\base\behavior,可以绑定到任意yii\base\compent实例上,然后这 ...