109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)

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

struct ListNode {
   int val;
   ListNode *next;
   ListNode(int x) : val(x), next(NULL) {}
 };
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;
        int size = 1;
        ListNode * listNode = head->next;
        while(listNode)
        {
            size++;
            listNode = listNode->next;
        }
        TreeNode* root = new TreeNode(0);
        binarySearch(head,size,root);
        return root;
    }
    void binarySearch(ListNode *head,int size, TreeNode* treeNode) //二分法,对于Array,需要一头一尾两个指针;对于List需要头指针及size
    {
        if(size == 1)
        {
            treeNode->val = head->val;
            return;
        }
        ListNode * listNode = head;
        int mid = size>>1;
        for(int i = 1 ; i<mid;i++)
        {
            listNode = listNode ->next;
        }
        treeNode->val = listNode->next->val;
        ListNode* head2 = listNode->next->next;
        listNode->next = NULL;
        treeNode->left = new TreeNode(0); //先申请号空间,再传递。否则在被调用函数中指针本身值的变化并不会影响调用者
        binarySearch(head,mid,treeNode->left);
        if(head2)
        {
            treeNode->right = new TreeNode(0);
            binarySearch(head2,size-mid-1,treeNode->right);
        }
    }
};

也可以通过引用传递,这样就不需要先初始化。

class Solution {
public:
    TreeNode *sortedListToBST(ListNode *head) {
        if(!head) return NULL;
        int size = 1;
        ListNode * listNode = head->next;
        while(listNode)
        {
            size++;
            listNode = listNode->next;
        }
        TreeNode* root = new TreeNode(0);
        binarySearch(head,size,root);
        return root;
    }
    void binarySearch(ListNode *head,int size, TreeNode* &treeNode) //按引用传递参数
    {
        if(size == 1)
        {
            treeNode=new TreeNode(head->val);
            return;
        }
        ListNode * listNode = head;
        int mid = size>>1;
        for(int i = 1 ; i<mid;i++)
        {
            listNode = listNode ->next;
        }
        treeNode = new TreeNode (listNode->next->val); //在被调用处申请空间
        ListNode* head2 = listNode->next->next;
        listNode->next = NULL;
        binarySearch(head,mid,treeNode->left); //引用的是0x0000的地址
        if(head2)
        {
            binarySearch(head2,size-mid-1,treeNode->right);
        }
    }
};

注意,NULL是一个宏定义

#define NULL 0

即NULL = (void*) 0

时间: 2024-07-29 02:20:13

109. Convert Sorted List to Binary Search Tree (List; Divide-and-Conquer, dfs)的相关文章

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

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

【一天一道LeetCode】#109. Convert Sorted List to Binary Search Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. (二)解题 本题大意:给定一个单向链表,构造出平衡二叉搜索树 解题思路:参考[一天一道Leet

[leedcode 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. /** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */ /** * D

【LeetCode】109&amp; - Convert Sorted List to Binary Search Tree&amp;Convert Sorted Array to Binary Search Tree

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Solution 1:  recursion runtime: 28ms. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(i

LeeCode 109.Convert Sorted List to Binary Search Tree(将排序链表转化为BST) 解题思路和方法

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:此题与排序数组很大的不同是链表不知道长度以及上面的值.其整体思路还是中间值作为根节点,但是需要一点策略,不然就会超时. 先整体遍历长度之后,将长度保存,这样就不需要每次都遍历链表了. 代码如下: /** * Definition for singly-linked list

【Leetcode】109. Convert Sorted List to Binary Search Tree

Question: 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 of the two subtrees of every node n

Java for LeetCode 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. 解题思路: 同上题,JAVA实现如下: public TreeNode sortedListToBST(ListNode head) { ArrayList<Integer> list=new ArrayList<Integer>(); while(head!=nu

109. Convert Sorted List to Binary Search Tree

有点没有完全想明白. 能够理解的点有: 1. 给你一个sorted list,用这里面的树构建bst,说明这个list是该bst的inorder遍历. 2. 给你的list相当于一个queue,每次用掉一个node就往后移动一格,相当于queue.poll(); 3. 和之前那题serialize的题有点像,我当时选择的方式是preorder,这里是Inorder.但是那个里面叶节点会用#标记,这里没有#, 只能用一个start, end来标记,如果start>end那么就说明已经到叶节点了,返