5 Convert Sorted List to Binary Search Tree_Leetcode

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

这题和sort list等题都比较相似,需要先用快慢指针的方法找到链表的中点,然后用recursive的方式构建左子树和右子树(用到的思想是Divide&Conquer),然后再构建好这个节点。

编程时一点要注意:

(1)dummy节点的使用可以帮助找到中点的prev节点

但是dummy节点只有当用时再构造,建议不要提前建好当参数传进来,会非常不清晰。用完后最后记得delete,否则会memory leak. 也不能delete得过早,因为prev可能等于dummy.

(2)边界情况

当middle节点为head时,应注意,左子树为NULL,不要递归build。

Code:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
/**
 * Definition for binary tree
 * 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) return NULL;
        TreeNode* treeHead = buildTree(head);
        return treeHead;
    }

    TreeNode* buildTree(ListNode *head)
    {
        if(!head) return NULL;
        if(!head->next) return new TreeNode(head->val);

        ListNode* dummy = new ListNode(0);
        dummy->next = head;
        ListNode* prev = dummy, *slow = dummy->next, *fast = dummy->next->next;
        while(fast && fast->next)
        {
            slow = slow->next;
            prev = prev->next;
            fast = fast->next->next;
        }

        TreeNode* cur = new TreeNode(slow->val);

        TreeNode* left, *right;  // second error, cannot initialize left in the if-else
        right = buildTree(slow->next);
        if(prev != dummy)      // first error
        {
            prev->next = NULL;
            left = buildTree(head);
        }
        else left = NULL;

        cur->left = left;
        cur->right = right;

        delete dummy;  // first error, can not delete dummy too early if prev equals dummy

        return cur;
    }
};

  

时间: 2024-11-13 08:19:46

5 Convert Sorted List to Binary Search Tree_Leetcode的相关文章

leetcode 109 Convert Sorted List to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ Convert Sorted List to Binary Search Tree Description Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. /** * De

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

39.1: Convert Sorted Array to Binary Search Tree

/************************************************************************/            /*       39.1:  Convert Sorted Array to Binary Search Tree                               */            /************************************************************

Convert Sorted Array to Binary Search Tree & Convert Sorted List to Binary Search Tree

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 /**

leetcode dfs Convert Sorted List to Binary Search Tree

Convert Sorted List to Binary Search Tree Total Accepted: 21420 Total Submissions: 78476My Submissions Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题意:把有序单链表转换为二叉查找树 思路: 用链表中心作为当作二叉树的根,

[leetcode]Convert Sorted List to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-list-to-binary-search-tree/ 题意:将一条排序好的链表转换为二叉查找树,二叉查找树需要平衡. 解题思路:两个思路:一,可以使用快慢指针来找到中间的那个节点,然后将这个节点作为树根,并分别递归这个节点左右两边的链表产生左右子树,这样的好处是不需要使用额外的空间,坏处是代码不够整洁.二,将排序好的链表的每个节点的值存入一个数组中,这样就和http://www.cnblog

[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. 这道题是要求把有序链表转为二叉搜索树,和之前那道Convert Sorted Array to Binary Search Tree 将有序数组转为二叉搜索树思路完全一样,只不过是操作的数据类型有所差别,一个是数组,一个是链表.数组方便就方便在可以通过index直接访问任意一个元

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

109.Convert Sorted List to Binary Search Tree Leetcode Python

Convert Sorted List to Binary Search Tree Total Accepted: 32343 Total Submissions: 117376 My Submissions Question Solution Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST 这题和convert array t