LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]

1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

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

这里两道题目。是连在一起的两题。给你一个排好序(升序)的数组或者链表,将它们转为一棵平衡二叉树。如果不排好序的话。一组随机输入的数据,就必须採用RBT或者AVL树,这样操作会变得更复杂。涉及到旋转,可是这里排好序了。所以,仅仅要找到中位数。作为根,然后递归地依据中位数的左、右数列来构建左右子树;

两题的思路都是如上所述, 唯一的差别就是,链表寻找中位数会麻烦一些。须要引入fast、slow两个指针,来寻找中位数。代码例如以下:

1.Array

class Solution {
public:
    TreeNode *Tree(int left, int right, vector<int> &num){
        TreeNode *root = NULL;
        if (left <= right){
            int cen = (left + right) / 2;
            root = new TreeNode(num[cen]);
            root->left = Tree(left, cen - 1, num);
            root->right = Tree(cen + 1, right, num);
        }
        return root;
    }

    TreeNode *sortedArrayToBST(vector<int> &num) {
        TreeNode *T = NULL;
        int N = num.size();
        T = Tree(0, N - 1, num);
        return T;
    }
};

2.link-list

class Solution {
public:
    ListNode *findMid(ListNode *head){        //这里假设链表中仅仅有两个数字。则mid返回的是head->next.
        if (head == NULL || head -> next == NULL)
            return head;
        ListNode *fast, *slow, *pre;
        fast = slow = head;
        pre = NULL;
        while (fast && fast->next){
            pre = slow;
            slow = slow->next;
            fast = fast->next->next;
        }
        pre->next = NULL;
        return slow;
    }

    TreeNode *buildTree(ListNode *head){
        TreeNode *root = NULL;
        ListNode *mid = NULL;
        if (head){
            mid = findMid(head);
            root = new TreeNode(mid->val);
            if (head != mid){
                root->left = buildTree(head);
                root->right = buildTree(mid->next);
            }
        }
        return root;

    }

    TreeNode *sortedListToBST(ListNode *head) {
        TreeNode *T;
        T = buildTree(head);
        return T;
    }
};
时间: 2024-12-22 15:55:34

LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]的相关文章

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class

LeetCode: Convert Sorted Array to Binary Search Tree [108]

[题目] Given an array where elements are sorted in ascending order, convert it to a height balanced BST. [题意] 给定一个已排序的数组(不存在重复元素),将它转换成一棵平衡二叉搜索树. [思路] 由于平衡二叉树要求左右子树的高度差绝对值相遇等于1,也就是说左右子树尽可能包含相同数目节点. 则使用二分法来解本题即可. [代码] /** * Definition for binary tree *

Convert Sorted double linked List to Binary Search Tree

Convert Sorted double linked List to Binary Search Tree In place 闫老师讲的这两个 答案 是把他 看成 single linked list了, 但是对于double linked list , 也适用 base case : at least three nodes , other wise mid.next and mid.next.next NPE NOT TESTED YET 小班文档 Solution one: 先处理 h

LeetCode: Convert Sorted Array to Binary Search Tree 解题报告

Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending order, convert it to a height balanced BST.SOLUTION 1:使用递归解决. base case: 当索引值超过时,返回null. 递归主体:构造根节点,调用递归构造左右子树.并将左右子树挂在根节点上. 返回值:返回构造的根节点. GITHUB: https:

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. 原题链接:https://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题目:给定一个升序排列元素的数组,将其转换成高度平衡的二叉树. 思路:已经排序.则从中间劈开,中间元素为树的根,左右递归构建. public

[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. 这道题是要将有序数组转为二叉搜索树,所谓二叉搜索树,是一种始终满足左<根<右的特性,如果将二叉搜索树按中序遍历的话,得到的就是一个有序数组了.那么反过来,我们可以得知,根节点应该是有序数组的中间点,从中间点分开为左右两个有序数组,在分别找出其中间点作为原中间点的左右两个子节点,这不就是是二分查找法的核

LeetCode: Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List to Binary Search Tree

Title: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Title: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:自顶向下 /** * Definition for

[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 *mergeAToT(vector<int> &num,int left,int right){ 4 if(left>right) 5 return NULL; 6 int

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra