• 单向链表转平衡二叉搜索树


    题目描述

    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) {
            if(head == NULL)
                return NULL; 
            
            vector<int> temp; 
            while(head != NULL)
            {
                temp.push_back(head->val); 
                head = head->next; 
            }
            
            return vecToBST(temp); 
            
        }
        
    private: 
        TreeNode* vecToBST(vector<int> &node)
        {
            if(node.size() == 0)
                return NULL; 
            
            int len = node.size(); 
            TreeNode *root = new TreeNode(node[ len/2 ]); 
            vector<int> left(node.begin(), node.begin() + len/2); 
            vector<int> right(node.begin() + len/2 + 1, node.end()); 
            root->left = vecToBST(left); 
            root->right = vecToBST(right); 
            
            return root;
        }
    };
    


    参考别人的代码

    (平衡)二叉树搜索树的中序变量可以是一个排了序的单向链表, 可以用左右子树的节点个数,控制二叉搜索树是一个平衡二叉搜索树。

    /**
     * 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 {
    private:
        ListNode* list; 
        
    public:
        TreeNode* sortedListToBST(ListNode* head) {
            if(head == NULL)
                return NULL; 
            
            list = head; 
            return generate(count(head)); 
        }
        
    private:
        // 计数
        int count(ListNode* pos)
        {
            int size = 0; 
            while(pos != NULL)
            {
                size++; 
                pos = pos->next; 
            }
            
            return size; 
        }
        
        // 按照计数和中序遍历构造平衡二叉搜索树
        TreeNode* generate(int n)
        {
            if(n == 0)
                return NULL; 
            
            TreeNode* node = new TreeNode(0); 
            node->left = generate(n/2); 
            node->val = list->val; 
            list = list->next; 
            node->right = generate(n - n/2 - 1); 
            
            return node; 
        }
    };
    
    


  • 相关阅读:
    根据用户行为发送客服消息
    tp5链接访问
    统计每天分享次数总和
    csv中文乱码
    Jupyter Notebook命令
    Laplace(拉普拉斯)算子
    Sobel算子
    Opencv图像连通域
    Opencv常用函数讲解
    12306 Pytho抢票代码
  • 原文地址:https://www.cnblogs.com/acm1314/p/7118982.html
Copyright © 2020-2023  润新知