[LeetCode]109. Convert Sorted List to Binary Search Tree
来源:互联网 发布:tomcat java options 编辑:程序博客网 时间:2024/06/17 07:04
[LeetCode]109. Convert Sorted List to Binary Search Tree
题目描述
思路
和 [LeetCode]108. Convert Sorted Array to Binary Search Tree 问题思路相同,只是数据结构从数组变成了链表,找到链表重点的方法,需要用快慢指针
代码
#include <iostream>#include <vector>using namespace std;struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {}};struct TreeNode { int val; TreeNode* left; TreeNode* right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public: ListNode* vectorToList(vector<int> nums) { if (nums.size() == 0) return NULL; ListNode *head = new ListNode(nums[0]), *iter = head; for (int i = 1; i < nums.size(); ++i) { ListNode *temp = new ListNode(nums[i]); iter->next = temp; iter = iter->next; } return head; } TreeNode* sortedListToBST(ListNode* head) { return insert(head, NULL); } TreeNode* insert(ListNode* head, ListNode* tail) { if (head == NULL || head == tail || (tail && tail->next == head)) return NULL; ListNode *slow = head, *fast = head; while (fast != tail) { if (fast == tail || fast->next == tail) break; slow = slow->next; fast = fast->next->next; } TreeNode *root = new TreeNode(slow->val); root->left = insert(head, slow); root->right = insert(slow->next, tail); return root; } void visit(TreeNode* root) { if (root == NULL) return; if (root->left) visit(root->left); cout << root->val << " "; if (root->right) visit(root->right); }};int main() { vector<int> nums = { 1,2,3,4,5,6,7 }; Solution s; ListNode *head = s.vectorToList(nums); TreeNode *root = s.sortedListToBST(head); s.visit(root); cout << endl; system("pause"); return 0;}
0 0
- LeetCode 109. Convert Sorted List to Binary Search Tree
- [LeetCode]109.Convert Sorted List to Binary Search Tree
- 109.Convert Sorted List to Binary Search Tree Leetcode Python
- [Leetcode] 109. Convert Sorted List to Binary Search Tree
- LeetCode --- 109. Convert Sorted List to Binary Search Tree
- [leetcode] 109.Convert Sorted List to Binary Search Tree
- 109. Convert Sorted List to Binary Search Tree LeetCode
- [leetcode] 109. Convert Sorted List to Binary Search Tree
- Leetcode 109. Convert Sorted List to Binary Search Tree
- [LeetCode]109. Convert Sorted List to Binary Search Tree
- LeetCode *** 109. Convert Sorted List to Binary Search Tree
- LeetCode 109. Convert Sorted List to Binary Search Tree
- LeetCode - 109. Convert Sorted List to Binary Search Tree
- Leetcode 109. Convert Sorted List to Binary Search Tree
- [leetcode] 109. Convert Sorted List to Binary Search Tree
- [LeetCode] 109. Convert Sorted List to Binary Search Tree
- Leetcode-109. Convert Sorted List to Binary Search Tree
- 【leetcode】109. Convert Sorted List to Binary Search Tree
- Windows HTTP服务(WinHTTP)介绍
- 文本关键词提取算法
- Kettle乱码解决方案
- [Maven]在Eclipse中执行并添加Maven命令
- java笔记
- [LeetCode]109. Convert Sorted List to Binary Search Tree
- 安卓中的上下文操作模式ActionMode:email列表多选中用到的
- npm和webpack下的angular-demo
- MLlib
- Java总结篇系列:Java多线程(三)
- 对象在内存中是如何存储的(内存五大区域和类加载)
- 336. Palindrome Pairs
- http://blog.csdn.net/harvic880925/article/details/40660137
- 验证两个文件是否一致