LintCode 106: Convert Sorted List to Binary Search Tree (并不简单)

本文介绍了一种将升序链表转换为高度平衡的二叉搜索树的方法。通过双指针技术找到链表的中点,并递归地构建左右子树,最终形成平衡的二叉搜索树。代码实现细节复杂,需注意链表操作和递归过程。
  1. Convert Sorted List to Binary Search Tree
    中文English
    Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST.

Example
Example 1:
Input: array = [1,2,3]
Output:
2
/
1 3

Example 2:
Input: [2,3,6,7]
Output:
3
/
2 6

7

Explanation:
There may be multi answers, and you could return any of them.

解法1:思路:先用双指针找到中间节点,然后递归。时间复杂度O(nlogn)。
注意:这种有链表操作又有递归的情况代码并不好写,容易出错。需要特别仔细。

  1. 以p1为节点,但是p1从while出来后其前节点pre要保存下来。然后pre->next=NULL,这样就相当于把两个子链表断开了。要不然左子链表一直到整个链表结束才结束。

代码如下:

/**
 * Definition of ListNode
 * class ListNode {
 * public:
 *     int val;
 *     ListNode *next;
 *     ListNode(int val) {
 *         this->val = val;
 *         this->next = NULL;
 *     }
 * }
 * Definition of TreeNode:
 * class TreeNode {
 * public:
 *     int val;
 *     TreeNode *left, *right;
 *     TreeNode(int val) {
 *         this->val = val;
 *         this->left = this->right = NULL;
 *     }
 * }
 */


class Solution {
public:
    /*
     * @param head: The first node of linked list.
     * @return: a tree node
     */
    TreeNode * sortedListToBST(ListNode * head) {
        if (!head) return NULL;
        
        return helper(head);
    }
    
private:
    TreeNode * helper(ListNode * head) {
        if (!head) return NULL;
        if (!head->next) return new TreeNode(head->val);
        
        ListNode * p1 = head, * p2 = head, * pre = head;

        while(p2 && p2->next) {
            pre = p1;
            p1 = p1->next;
            p2 = p2->next->next;
            
        }
        //now p1 is the mid of the list;
        TreeNode * mid = new TreeNode(p1->val);
        pre->next = NULL;
        mid->left = helper(head);
        mid->right = helper(p1->next);

        return mid;
    }
};

稍微改了一下,这种写法也可以。

   
    TreeNode * helper(ListNode * head) {
        if (!head) return NULL;
        if (!head->next) return new TreeNode(head->val);
        
        ListNode * p1 = head, * p2 = head, * pre = head;
        while(p2 && p2->next) {
            pre = p1;
            p1 = p1->next;
            p2 = p2->next;
            if (p2) p2 = p2->next;
        }
        //now p1 is the mid of the list;
        TreeNode * mid = new TreeNode(p1->val);
        pre->next = NULL;
        mid->left = helper(head);
        mid->right = helper(p1->next);
        return mid;
    }

这个写法也可以:以这个版本为准比较好。

    TreeNode * helper(ListNode * head) {
        if (!head) return NULL;
        if (!head->next) return new TreeNode(head->val);
        
        ListNode * p1 = head, * p2 = head->next, * pre = head;

        while(p2) {
            pre = p1;
            p1 = p1->next;
            p2 = p2->next;
            if (p2) p2 = p2->next;
        }
        //now p1 is the mid of the list;
        TreeNode * mid = new TreeNode(p1->val);
        pre->next = NULL;
        mid->left = helper(head);
        mid->right = helper(p1->next);

        return mid;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值