Convert Sorted Array to Binary Search Tree
来源:互联网 发布:smtp.126.com 端口号 编辑:程序博客网 时间:2024/06/10 08:18
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
#include<iostream>#include<vector>using namespace std;struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x), left(NULL), right(NULL) {}};TreeNode * CreatBST(vector<int> &num, int first, int last){if (first > last)return NULL;int mid = (first + last) / 2;TreeNode* curnode = new TreeNode(num[mid]);curnode->left = CreatBST(num, first, mid - 1);curnode->right = CreatBST(num, mid + 1, last);return curnode;}TreeNode *sortedArrayToBST(vector<int> &num) {if (num.empty())return NULL;return CreatBST(num, 0, num.size() - 1);}
0 0
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- Convert Sorted Array to Binary Search Tree
- 第四周 【项目1-三角形类的构造函数】<3>
- Android 内存溢出心得
- Linux下动态库查找路径的问题
- POJ 3260 The Fewest Coins(多重背包+完全背包)
- Ubuntu 如何安装pip
- Convert Sorted Array to Binary Search Tree
- flexpaper 无法加载flash问题解决
- CentOS 7安装Matlab R2014b
- 数模转换(ADC)的应用笔记(1)
- 2015浙工大校赛-Problem F: 数数字(map)
- RabbitMQ消息队列(一): Detailed Introduction 详细介绍(转)
- Unix环境编程中的apue.h和err_quit、err_sys问题
- php正则匹配
- 在 CentOS 7 / RHEL 7 上怎样安装 Eclipse Luna IDE