zoukankan      html  css  js  c++  java
  • 108. Convert Sorted Array to Binary Search Tree 109. Convert Sorted List to Binary Search Tree -- 将有序数组或有序链表转成平衡二叉排序树

    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.

    /**
     * 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* buildTree(vector<int> nums, int left, int right)
        {
            if(left > right)
                return NULL;
            TreeNode *node;
            if(left == right)
            {
                node = new TreeNode(nums[left]);
                return node;
            }
            int mid = (left+right)/2;
            node = new TreeNode(nums[mid]);
            node->left = buildTree(nums, left, mid-1);
            node->right = buildTree(nums, mid+1, right);
            return node;
        }
        
        TreeNode* sortedArrayToBST(vector<int>& nums) {
            int n = nums.size();
            if(!n)
                return NULL;
            TreeNode *root = buildTree(nums, 0, n-1);
            return root;
        }
    };

    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.
     * 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* buildTree(ListNode* &head, int left, int right)
        {
            if(left > right)
                return NULL;
            int mid = (left + right) / 2;
            TreeNode *leftNode = buildTree(head, left, mid-1);
            TreeNode *node = new TreeNode(head->val);
            node->left = leftNode;
            head = head->next;
            TreeNode *rightNode = buildTree(head, mid+1, right);
            node->right = rightNode;
            return node;
        }
        
        TreeNode* sortedListToBST(ListNode* head) {
            int len = 0;
            for(ListNode *p = head; p; p = p->next)
                len++;
            if(!len)
                return NULL;
            TreeNode *root = buildTree(head, 0, len-1);
            return root;
        }
    };

    先建左子树,然后head后移,再建右子树。

  • 相关阅读:
    python学习笔记----正则表达式
    MongoDB系统CentOS 7.1 crash的排障过程
    MySQL实例crash的案例分析
    关于MySQL连接抛出Authentication Failed错误分析
    Redis偶发连接失败案例分析
    MYSQL NULL值特性
    一个MySQL 5.7 分区表性能下降的案例分析
    Linux 桥接网络不自动分配IP的问题
    Docker浅析
    配置Servlet 容器
  • 原文地址:https://www.cnblogs.com/argenbarbie/p/5415876.html
Copyright © 2011-2022 走看看