LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height
2015-11-27 11:09
363 查看
C++
/** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param A: A sorted (increasing order) array * @return: A tree node */ TreeNode *sortedArrayToBST(vector<int> &A) { // write your code here if (0 == A.size()) { return NULL; } return buildTree(A, 0, A.size()-1); } TreeNode *buildTree(vector<int> &A, int from, int to) { if (from > to) { return NULL; } int mid = (from+to)/2; TreeNode *node = new TreeNode(A[mid]); node->left = buildTree(A, from, mid-1); node->right = buildTree(A, mid+1, to); return node; } };
相关文章推荐
- iOS开发--一些UITabBarItem属性的设置[转]
- 微信支付 —— 公众号支付代码详解(1/7)
- 南通大学教务管理微信公众号的用户体验
- Rust语言中文版教程
- django log(续 续)
- win7休眠的开启与关闭方法
- 使用exe4j制作swing发布包
- 类Dota2服务器架构
- [ActionScript 3.0] Away3D 灯光的使用
- Linux系统中tr命令删除和替换文本字符的基本用法
- 记录一下关于融云推送消息
- linux系统管理之安装操作系统rhel7.1
- Android读取SeekBar数值方法
- 一致性hash算法 - consistent hashing
- android联系人获取,添加
- OpenCV 2.4+ C++ SVM线性不可分处理
- 网上办公室-Tower
- linux nc命令常用用法
- LintCode: Binary Tree Paths
- 图像主题色提取算法