Convert Sorted Array to Binary Search Tree
来源:互联网 发布:签名设计软件下载 编辑:程序博客网 时间:2024/05/11 04:03
思想:二分法建立平衡二叉树,和用链表建立的思想一样,处理过程大致相同
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode *BalanceBst(vector<int> &num, int beg, int end){
if(beg > end)
return NULL;
int mid = (beg+end)>>1;
TreeNode *root = new TreeNode(num[mid]);
root->left = BalanceBst(num, beg, mid-1);
root->right = BalanceBst(num, mid+1, end);
return root;
}
TreeNode *sortedArrayToBST(vector<int> &num) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
return BalanceBst(num, 0, num.size()-1);
}
};
- 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
- CocosBuilder使用说明文档
- 一点关于Namp及Nikto的记录
- 相机变换矩阵
- Convert Sorted List to Binary Search Tree
- Monkeyrunner 使用说明
- Convert Sorted Array to Binary Search Tree
- java字符串的连接方法比较
- java判断时间的先后
- ORA-23421: job number 25 is not a job in the job queue
- HDU 3415单调队列
- ACM 进阶学习第一课----同余相关之欧几里得算法及其扩展(2)
- ORA-12514&GLOBAL_DBNAME & SERVICE_NAME
- 二、对象的所在地——容器
- Dyninst学习笔记