<LeetCode OJ> 109 / 108 Convert Sorted ( List / Array ) to Binary Search Tree
2016-06-02 21:48
656 查看
Total Accepted: 71642 Total
Submissions: 233074 Difficulty: Medium
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
Hide Tags
Depth-first Search Linked
List
Hide Similar Problems
(M) Convert Sorted Array to Binary Search Tree
分析:
有序单表链转化成二叉树,本题和有序数组转化成二叉搜索树差别还是有的,但是可以先将链表转化成数组就成为上一题的模式了!但是这样做恐怕没有达到考察目的!
学习别人的算法:
快慢指针寻找链表的中间位置,原来此题就像考察这个!!!才明白!
Total Accepted: 77544 Total
Submissions: 206040 Difficulty: Medium
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first
Search
Hide Similar Problems
(M) Convert Sorted List to Binary Search Tree
分析:
有序数组转化成二叉搜索树。本题解题思路还是比较明显的!因为数组有序,为了能平衡,总是选择数组(未被使用过的位置的)中间位置的值作为当前二叉树的根节点!出门看了一下别人的做法,尽然不谋而合!
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/51570438
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:/article/3664871.html
Submissions: 233074 Difficulty: Medium
Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
Hide Tags
Depth-first Search Linked
List
Hide Similar Problems
(M) Convert Sorted Array to Binary Search Tree
分析:
有序单表链转化成二叉树,本题和有序数组转化成二叉搜索树差别还是有的,但是可以先将链表转化成数组就成为上一题的模式了!但是这样做恐怕没有达到考察目的!
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ /** * 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* sortedListToBST(ListNode* head) { if( head == NULL ) return NULL; vector<int> nums; while(head) { nums.push_back(head->val); head=head->next; } return creatTree(nums,0,nums.size()-1); } TreeNode* creatTree(vector<int>& nums,int leftpos,int rightpos) { if(leftpos > rightpos) return NULL; int midpos= (leftpos+rightpos)/2; TreeNode* newnode=new TreeNode(nums[midpos]); newnode->right=creatTree(nums,midpos+1,rightpos); newnode->left=creatTree(nums,leftpos,midpos-1); return newnode; } };
学习别人的算法:
快慢指针寻找链表的中间位置,原来此题就像考察这个!!!才明白!
class Solution { public: TreeNode* sortedListToBST(ListNode* head) { if(head==NULL) return NULL; if(head->next==NULL) return new TreeNode(head->val); return creatTree(head,NULL); } TreeNode* creatTree(ListNode* head,ListNode* tail) { if(head==tail) return NULL; //head是起始节点,tail是终止结点的下一个结点 ListNode* slow=head; ListNode* fast=head; while(fast!=tail && fast->next!=tail)//通过快慢指针快速找到根节点 { fast=fast->next->next; slow=slow->next; } TreeNode* res=new TreeNode(slow->val); res->left=creatTree(head,slow);//递归的构建左子树,右子树 res->right=creatTree(slow->next,tail); return res; } };
Total Accepted: 77544 Total
Submissions: 206040 Difficulty: Medium
Given an array where elements are sorted in ascending order, convert it to a height balanced BST.
Subscribe to see which companies asked this question
Hide Tags
Tree Depth-first
Search
Hide Similar Problems
(M) Convert Sorted List to Binary Search Tree
分析:
有序数组转化成二叉搜索树。本题解题思路还是比较明显的!因为数组有序,为了能平衡,总是选择数组(未被使用过的位置的)中间位置的值作为当前二叉树的根节点!出门看了一下别人的做法,尽然不谋而合!
/** * 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() == 0 ) return NULL; return creatTree(nums,0,nums.size()-1); } TreeNode* creatTree(vector<int>& nums,int leftpos,int rightpos) { if(leftpos > rightpos) return NULL; int midpos= (leftpos+rightpos)/2; TreeNode* newnode=new TreeNode(nums[midpos]); newnode->right=creatTree(nums,midpos+1,rightpos); newnode->left=creatTree(nums,leftpos,midpos-1); return newnode; } };
注:本博文为EbowTang原创,后续可能继续更新本文。如果转载,请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/51570438
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:/article/3664871.html
相关文章推荐
- 总结
- 总结
- 解析Tensorflow官方PTB模型的demo
- sql技巧-构造分组条件
- 11、python标准库概览
- xcode插件入门教程
- 【转载】C++资源之不完全导引
- ubuntu apt-get详情
- Oracle11gR2_RAC_for_redhat6.3 安装手册
- tar命令基本用法
- Ubuntu 常用命令
- redis sadd存缓存
- 图像的去模糊
- makefie 之赋值
- 安卓初识基本控件_Dialog对话框
- window 下mysql5.7.12安装报错记录
- linear_regression
- 12种排序算法:原理、图解、动画视频演示、代码以及笔试面试题目中的应用
- 剑指offer-29数组中出现次数超过一半的数字
- Java多线程 之 Callable sleep yield 优先级(三)