Convert Sorted List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s

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

基本思路:

中序遍历的过程,与有序链表的顺序是一一对应的。

采用中序遍历构造进树的构造。

并利用取值范围,确定,根的位置,以及子树的范围。

故需要遍历整个链表,求得总的长度。

链表长度的一半,即为根结点所在位置。 左边则为左子树的取值范围,右边即为右子树的取值范围。

同上,递归应用到左子树,右子树。逐步缩范围,直到叶节点。

/**
 * 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) {
        int size = 0;
        ListNode *p = head;
        while (p) {
            ++size;
            p = p->next;
        }

        return helper(head, 0, size-1);
    }

    TreeNode *helper(ListNode *&head, int start, int stop) {
        if (start > stop) return 0;
        const int mid = start + (stop-start) / 2;
        TreeNode *left = helper(head, start, mid-1);
        TreeNode *root = new TreeNode(head->val);
        root->left = left;
        head = head->next;
        root->right = helper(head, mid+1, stop);
        return root;
    }
};
时间: 2024-07-29 07:30:20

Convert Sorted List to Binary Search Tree -- leetcodeGiven a singly linked list where elements are s的相关文章

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 -day19 Convert Sorted List to Binary Search Tree

1.  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. 分析:将一个升序排列的链表转换为平衡二叉搜索树,采用递归的方式,先找到链表的中点,作为二叉树的根,然后递归求解左右子树. 如下: class Solution { public: Tr

[Leetcode][JAVA] 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. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,

LeetCode解题报告——Convert Sorted List to Binary Search Tree & Populating Next Right Pointers in Each Node & Word Ladder

1. 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. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth

leetcode-algorithms-109. Convert Sorted List to Binary Search Tree

leetcode-algorithms-109. 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. For this problem, a height-balanced binary tree is defined as a binary tr

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

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. 题意:把有序单链表转换为二叉查找树 思路: 用链表中心作为当作二叉树的根,