108. Convert Sorted Array to Binary Search Tree
2016-03-14 13:09
369 查看
/**32ms ac * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ class Solution { public: TreeNode* sortedArrayToBST(vector<int>& nums) { if(!nums.size()) return NULL; int len = nums.size(); TreeNode* a=new TreeNode(nums[len/2]); vector<int> nums1; vector<int> nums2; nums1.insert(nums1.end(),nums.begin(),nums.begin()+len/2); nums2.insert(nums2.end(),nums.begin()+len/2+1,nums.end()); a->left=sortedArrayToBST(nums1); a->right=sortedArrayToBST(nums2); return a; } };
相关文章推荐
- 107. Binary Tree Level Order Traversal II
- BZOJ3729: Gty的游戏
- 欧拉项目第14题 Longest Collatz sequence
- 104. Maximum Depth of Binary Tree(递归)
- 101. Symmetric Tree
- 100. Same Tree
- 94. Binary Tree Inorder Traversal
- 线程编程时,需要注意的
- PHP 数组
- 网络设备文件管理
- 复利计算器2.01
- 88. Merge Sorted Array
- 70. Climbing Stairs
- 67. Add Binary
- 66. Plus One
- Spring中LocalSessionFactoryBean与SessionFactory
- ajax请求Url加参数的使用方法
- ViurtualBox配置虚拟机Linux的网络环境
- Visual Stuido 2015 Community 使用 GitHub 插件
- 不小心打开了show children怎么关闭的问题