本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

LeetCode:Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST

分析:找到数组的中间数据作为根节点,小于中间数据的数组来构造作为左子树,大于中间数据的数组来构造右子树,递归解法如下

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  * int val;
 5  * TreeNode *left;
 6  * TreeNode *right;
 7  * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode *sortedArrayToBST(vector<int> &num) {
13         // IMPORTANT: Please reset any member data you declared, as
14         // the same Solution instance will be reused for each test case.
15         int len = num.size();
16         if(len == 0)return NULL;
17         return sortedArrayToBSTRecur(num, 0, len-1);
18     }
19     TreeNode *sortedArrayToBSTRecur(vector<int> &num, int istart, int iend)
20     {
21         if(istart > iend)return NULL;
22         int middle = (istart+iend)/2;
23         TreeNode *res = new TreeNode(num[middle]);
24         res->left = sortedArrayToBSTRecur(num, istart, middle-1);
25         res->right = sortedArrayToBSTRecur(num, middle+1, iend);
26         return res;
27     }
28 };

 


LeetCode:Convert Sorted List to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST

分析:和上一题同理,只不过要使用快慢指针来找到链表的中间节点                                                  本文地址

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 /**
10  * Definition for binary tree
11  * struct TreeNode {
12  *     int val;
13  *     TreeNode *left;
14  *     TreeNode *right;
15  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
16  * };
17  */
18 class Solution {
19 public:
20     TreeNode *sortedListToBST(ListNode *head) {
21         // IMPORTANT: Please reset any member data you declared, as
22         // the same Solution instance will be reused for each test case.
23         if(head == NULL)return NULL;
24         ListNode *fast = head, *slow = head, *preSlow = NULL;
25         while(fast->next && fast->next->next)
26         {
27             fast = fast->next->next;
28             preSlow = slow;
29             slow = slow->next;
30         }
31         TreeNode *res = new TreeNode(slow->val);
32         fast = slow->next;
33         delete slow;
34         if(preSlow != NULL)
35         {
36             preSlow->next = NULL;
37             res->left = sortedListToBST(head);
38         }
39         res->right = sortedListToBST(fast);
40         return res;
41     }
42 };

【版权声明】转载请注明出处:http://www.cnblogs.com/TenosDoIt/p/3440079.html

posted @ 2013-11-24 15:30  tenos  阅读(1120)  评论(0编辑  收藏  举报

本博客rss订阅地址: http://feed.cnblogs.com/blog/u/147990/rss

公益页面-寻找遗失儿童