LeetCode: Convert Sorted List to Binary Search Tree

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.

地址:https://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/

算法:把有序链表转化成平衡的二叉搜索树。首先找到链表的中间节点,然后把中间节点作为根,递归调用前面半个链表作为左子树,递归调用后面半个链表作为右子树。代码:

 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         if(!head){
22             return NULL;
23         }
24         if(!head->next){
25             TreeNode * p = new TreeNode(head->val);
26             return p;
27         }
28         ListNode *pre_first = NULL;
29         ListNode *first_p = head;
30         ListNode *second_p = head->next;
31         while(second_p){
32             pre_first = first_p;
33             first_p = first_p->next;
34             second_p = second_p->next;
35             if(second_p){
36                 second_p = second_p->next;
37             }
38         }
39         TreeNode *root = new TreeNode(first_p->val);
40         if(head != first_p){
41             pre_first->next = NULL;
42             root->left = sortedListToBST(head);
43         }
44         root->right = sortedListToBST(first_p->next);
45         return root;
46     }
47 };
 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         if(!head){
22             return NULL;
23         }
24         if(!head->next){
25             TreeNode * p = new TreeNode(head->val);
26             return p;
27         }
28         ListNode *pre_first = NULL;
29         ListNode *first_p = head;
30         ListNode *second_p = head->next;
31         while(second_p){
32             pre_first = first_p;
33             first_p = first_p->next;
34             second_p = second_p->next;
35             if(second_p){
36                 second_p = second_p->next;
37             }
38         }
39         TreeNode *root = new TreeNode(first_p->val);
40         if(head != first_p){
41             pre_first->next = NULL;
42             root->left = sortedListToBST(head);
43         }
44         root->right = sortedListToBST(first_p->next);
45         return root;
46     }
47 };

 

posted on 2014-08-27 21:16 Boostable 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/boostable/p/leetcode_convert_sorted_list_to_binary_search_tree.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值