• [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.

    Hide Tags
     Depth-first Search Linked List
     
     
    方法一:借助Convert Sorted Array to Binary Search Tree 的方法,先将list转成vector即可,时间复杂度O(n),转化过程就是O(n),空间复杂度O(n)
    class Solution {
        public:
            TreeNode *sortedListToBST(ListNode *head)
            {   
                vector<int> num;
                while(head != NULL)
                {   
                    num.push_back(head->val);
                    head = head->next;
                }   
                return sortedArrayToBST(num);
        
            }   
            TreeNode *sortedArrayToBST(vector<int> &num)
            {   
                int size = num.size();
                if(size == 0)
                    return NULL;
                 return sortedArrayToBSTInternal(num, 0, size - 1); 
    
            }   
    
            TreeNode *sortedArrayToBSTInternal(vector<int> &num, int low, int high)
            {   
    
                // the code is very important, i.e: low = 4, hight = 5, mid = 4, 
                // will call sortedArrayToBSTInternal(num, 4, 3)
                if(low > high)
                    return NULL;
                if(low == high)
                    return new TreeNode(num[low]);
    
                int mid = (high-low)/2 + low;
                cout << mid << endl;
                TreeNode *root = new TreeNode(num[mid]);
                TreeNode *left = sortedArrayToBSTInternal(num, low, mid - 1);
                TreeNode *right = sortedArrayToBSTInternal(num, mid + 1, high);
                root->left = left;
                root->right= right;
                return root;
            }
    };

    方法二:找出当前链表的中间节点,然后再递归左右的子链表,开始的时候程序先计算链表总厂,然后传入两个前后索引指针,最后每次递归找出中间节点即可。

    时间复杂度O(n^2),空间复杂度O(logn),递归导致stack用O(logn)

    /**
     * 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:
        int calLen(ListNode *node)
        {
            int len = 0;
            while(node)
            {
                len++;
                node = node->next;
            }
            return len;
        }
        
        TreeNode *createTree(ListNode *node, int left, int right)
        {
            if (left > right)
                return NULL;
                
            int mid = (left + right) / 2;
            
            ListNode *p = node;
            
            for(int i = left; i < mid; i++)
                p = p->next;
                
            TreeNode *leftNode = createTree(node, left, mid - 1);
            TreeNode *rightNode = createTree(p->next, mid + 1, right);
            
            TreeNode *tNode = new TreeNode(p->val);
            
            tNode->left = leftNode;
            tNode->right = rightNode;
            
            return tNode;        
        }
        
        TreeNode *sortedListToBST(ListNode *head) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            int len = calLen(head);
            return createTree(head, 0, len - 1);
        }
    };

    方法3:

      bottom-up,时间复杂度 O(n),空间复杂度 O(logn)

    http://blog.csdn.net/salutlu/article/details/24502109  中递归

    http://www.bwscitech.com/a/jishuzixun/javayuyan/2013/0930/15822.html

    存在一种自底向上 (bottom-up) 的方法,见 http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html

    class Solution {
        public:
            TreeNode *sortedListToBST(ListNode *head) {
                int len = 0;
                ListNode *p = head;
                while (p) {
                    len++;
                    p = p->next;
                }
                return sortedListToBST(head, 0, len - 1);
            }
        private:
            TreeNode* sortedListToBST(ListNode*& list, int start, int end) {
                if (start > end) return NULL;
                int mid = start + (end - start) / 2;
                TreeNode *leftChild = sortedListToBST(list, start, mid - 1);
                TreeNode *parent = new TreeNode(list->val);
                parent->left = leftChild;
                list = list->next;
                parent->right = sortedListToBST(list, mid + 1, end);
                return parent;
            }
    };
  • 相关阅读:
    浅析深度优先和广度优先遍历实现过程、区别及使用场景
    浅析为什么要用setTimeout模拟setInterval
    app弹出软键盘获取键盘高度不准确的原因及导致底部定位的元素无法贴近键盘的问题
    App平台iOS设备上因内存不足导致白屏、闪退的原因及其解决方案
    浅谈移动端开发技术
    浅析Console命令调试常用方法
    js正则表达式中的正向肯定预查和正向否定预查, 反向肯定和反向否定(这个翻译不准确)
    javascript的版本查看及js的历史
    【转】JS-正则表达式的反向引用
    【转】Linux虚拟网络设备之tun/tap
  • 原文地址:https://www.cnblogs.com/diegodu/p/4409885.html
Copyright © 2020-2023  润新知