【LeetCode】Convert Sorted List to Binary Search Tree
来源:互联网 发布:相片变成漫画图软件 编辑:程序博客网 时间:2024/06/07 00:54
题目描述:
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
将一个单向链表转换成二叉搜索树。
我的解题思路是用AVL树插入的方法来将链表中的元素一个个接上。因为链表是已经排序好的,每一个节点都大于前一个节点,因此新插入的节点都位于最右下的位置,同样进行旋转操作时只需要进行一次左旋就可以了。用栈来记录下到达最右下节点的路径,方便寻找父节点;用map来记录下路径上每个节点对应的左孩子深度,右孩子深度新插入的节点为0,每向上爬一个父节点则++。要注意爬到根时单独处理。
代码如下:
//Definition for singly-linked list.struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {}};//Definition for binary treestruct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) {}};class Solution {public:TreeNode *sortedListToBST(ListNode *head) {if (!head)return NULL;TreeNode *tNode = new TreeNode(head->val);TreeNode *root = tNode;stack<TreeNode*> path;//路径unordered_map<TreeNode*, int> ldepth;//与path对应节点的左孩子深度path.push(tNode);ldepth[tNode] = 0;head = head->next;while (head){TreeNode *front = path.top();//寻找最右下节点while (front->right){path.push(front->right);front = front->right;}//将新值接到右下节点,该节点左孩子深度为0front->right = new TreeNode(head->val);ldepth[front->right] = 0;//向上寻找最小非平衡二叉树int currright = 1;TreeNode *next(NULL);while (abs(ldepth[front] - currright) <= 1 && !path.empty()){next = front;path.pop();if (!path.empty())front = path.top();currright++;}//如果path为空,即该树仍然是平衡二叉树,则continue//否则向左旋转if (path.empty()){head = head->next;path.push(root);continue;}TreeNode *curr = front;path.pop();if (path.empty())root = next;else{front = path.top();front->right = next;}curr->right = next->left;next->left = curr;ldepth[next] = ldepth[curr] + 1;path.push(next);head = head->next;}return root;}};
0 0
- 【LeetCode】Convert Sorted Array to Binary Search Tree && Convert Sorted List to Binary Search Tree
- LeetCode[Tree]: Convert Sorted List to Binary Search Tree
- [leetcode][list][dfs] Convert Sorted List to Binary Search Tree
- LeetCode: Convert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- LeetCode : Convert Sorted List to Binary Search Tree
- [Leetcode] Convert Sorted List to Binary Search Tree
- Leetcode: Convert Sorted List to Balanced Binary Search Tree
- [LeetCode] Convert Sorted List to Binary Search Tree
- leetcode 66: Convert Sorted List to Binary Search Tree
- leetcode Convert Sorted List to Binary Search Tree
- LeetCode Convert Sorted List to Binary Search Tree
- leetcode Convert Sorted List to Binary Search Tree
- LeetCode:Convert Sorted List to Binary Search Tree
- [LeetCode]Convert Sorted List to Binary Search Tree
- [Leetcode]Convert Sorted List to Binary Search Tree
- [leetcode]Convert Sorted List to Binary Search Tree
- Leetcode: Convert Sorted List to Binary Search Tree
- cglib 动态代理实现AOP
- 遇到的一些Android小问题
- Windows下通过Cygwin使用OpenSSh
- STL hashmap
- Cube Stacking
- 【LeetCode】Convert Sorted List to Binary Search Tree
- PHP 爬虫 两篇转载
- 二插排序树
- hibernate demo
- 简单的vim配置文件
- eclipse django sae网站开发流程
- uva 537
- 5.3poj2964日历问题
- OrmLite动态创建表,一个实体类创建多张表的的偏招