【leetcode】Convert Sorted List to Binary Search Tree (middle)

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

思路:题目看上去好像很难,但实际上很简单,递归做就行,每次找到左右子树对应的子链表就行。一次AC。

class Solution {
public:
    TreeNode *sortedListToBST(ListNode *head) {
        if(head == NULL)
            return NULL;

        ListNode * fast = head, *slow = head, *slowpre = head; //分别是快指针、慢指针、慢指针前一个指针 慢指针的位置就是当前平衡树根节点的位置 中间值
        while(fast != NULL && fast->next != NULL)
        {
            fast = fast->next->next;
            slowpre = slow;
            slow = slow->next;
        }
        TreeNode * root = new TreeNode(slow->val);
        ListNode * left = (slow == head) ? NULL : head; //如果慢指针==头指针 则其左子树是空的
        ListNode * right = slow->next;
        slowpre->next = NULL; //左子树对应的链表末尾置null
        root->left = sortedListToBST(left);
        root->right = sortedListToBST(right);

        return root;
    }
};
时间: 2024-10-22 01:58:08

【leetcode】Convert Sorted List to Binary Search Tree (middle)的相关文章

【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. [解析] 分治:用快慢指针找到链表的中点,作为树的root,然后二分--中点之前的链表和中点之后的链表分别再构造二叉平衡树. /** * Definition for singly-linked list. * public class ListNode { * int

【leetcode】Convert Sorted Array to Binary Search Tree (easy)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序数组变二叉平衡搜索树,不难,递归就行.每次先序建立根节点(取最中间的数),然后用子区间划分左右子树. 一次就AC了 注意:new 结构体的时候对于 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x)

【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. 解答 题目要求将链表转化为二叉查找树.利用树的中序遍历的递归思想,对链表结点一个个进行访问,先对左子树进行递归,然后将当前结点作为根,迭代到下一个链表结点,最后在递归求出右子树即可 /** * Definition for singly-linked list. * pub

【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 class Solution { 2 public: 3 TreeNode *createTree(vector<int> &num,int begin,int end) 4 { 5 if(begin>end) return NULL; 6 int mid=(begi

109【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.

Leetcode 之Convert Sorted Array to Binary Search Tree(54)

思路很简单,用二分法,每次选中间的点作为根结点,用左.右结点递归. TreeNode* sortedArrayToBST(vector<int> &num) { return sortedArrayToBST(num.begin(), num.end()); } template<typename RandomAccessIterator> TreeNode* sortedArrayToBST(RandomAccessIterator first, RandomAccess

[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. 方法:为了使BST高度平衡,要找链表中的中值作为当前根节点. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

LeetCode 108. Convert Sorted Array to Binary Search Tree (有序数组转化为二叉搜索树)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh

【Leetcode】【Medium】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,可以通过设置左子树结点数等于或者比右子树结点数多1,来实现. 那么每次取数组的中间位置后一个值,作为根结点,数组左边元素的插入左子树,数组右边元素插入右子树,依次类推. 代码: 1 /** 2 * Defi