lintcode-排序列表转换为二分查找树-106
来源:互联网 发布:tiger水杯 知乎 编辑:程序博客网 时间:2024/06/06 13:11
/** * Definition of ListNode * class ListNode { * public: * int val; * ListNode *next; * ListNode(int val) { * this->val = val; * this->next = NULL; * } * } * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */class Solution { void Free(ListNode *head){ ListNode *p=head; while(p){ head=p->next; free(p); p=head; } } vector<int> ListToArr(ListNode *head){ //想法就是把链表的数据存储到数组中 vector<int> base; while(head){ base.push_back(head->val); head=head->next; } Free(head); return base; } TreeNode* helper(const vector<int> &arr,int left,int right){ if(left>right) return nullptr; int mid=(left+right)/2; TreeNode *root=new TreeNode(arr[mid]); root->left=helper(arr ,left,mid-1); root->right=helper(arr,mid+1,right); return root; }public: TreeNode *sortedListToBST(ListNode *head) { if(head==nullptr) return nullptr; vector<int> ret=ListToArr(head); return helper(ret,0,ret.size()-1); }};
0 0
- lintcode-排序列表转换为二分查找树-106
- LintCode 106-排序列表转换为二分查找树
- LintCode-排序列表转换为二分查找树
- LintCode:排序列表转换为二分查找树
- lintcode,排序列表转换为二分查找树
- lintcode 排序列表转换为二分查找树(
- LintCode 排序列表转换为二分查找树
- 排序列表转换为二分查找树-LintCode
- 排序列表转换为二分查找树-LintCode
- LINTCODE---排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 排序列表转换为二分查找树
- 题目:排序列表转换为二分查找树
- 排序列表转化为二分查找树
- Java实现-排列列表转换为二分查找树
- web技术概论第三周作业
- 01 JDK环境变量配置
- 解读NoSQL技术代表之作Dynamo
- UNITY 5.2 SystemInfo类
- Acitivity 跳转是否Destroy()问题
- lintcode-排序列表转换为二分查找树-106
- iOS定位
- <base>标签在html5中使用时……
- 黑马程序员——字符串
- VS9(vs2008) 下 Debug 显示 UTF8 字符串
- 关于spring mvc+mybatis+junit单元测试
- 最短路径—每一对点的最短距离——弗洛伊德
- 一步步学Qt,第七天-QPainter绘图QRect定位问题
- 第一篇博文,讲讲最近遇到的第一次git时遇到的问题