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;
}
};

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

  1. LeetCode:Convert Sorted Array to Binary Search Tree,Convert Sorted List to Binary Search Tree

    LeetCode:Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in asce ...

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

    Convert Sorted Array to Binary Search Tree Given an array where elements are sorted in ascending ord ...

  3. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  4. [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. 这道 ...

  5. [Algorithm] Count occurrences of a number in a sorted array with duplicates using Binary Search

    Let's say we are going to find out number of occurrences of a number in a sorted array using binary ...

  6. LeetCode——Convert Sorted Array to Binary Search Tree

    Description: Given an array where elements are sorted in ascending order, convert it to a height bal ...

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

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

    原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉 ...

  9. LeetCode Convert Sorted Array to Binary Search Tree(数据结构)

    题意: 将一个有序的数组建成一棵平衡的BST树. 思路: 因为数组已经有序,每次可以从中点开始建根,再递归下去分别处理左/右子树. /** * Definition for a binary tree ...

随机推荐

  1. cocos2d-x 3.0游戏实例学习笔记《卡牌塔防》第八部---怪物出场

    /* 说明: **1.本次游戏实例是<cocos2d-x游戏开发之旅>上的最后一个游戏,这里用3.0重写并做下笔记 **2.我也问过木头本人啦,他说:随便写.第一别全然照搬代码.第二能够说 ...

  2. ConditonHelper

    在网上其实已经有很多类似这种拼接sql条件的类,但是没有看到一个让我感觉完全满意的这样的类.最近看到 http://www.cnblogs.com/xtdhb/p/3811956.html 这博客,觉 ...

  3. SE 2014年4月1日

    一. 描述OSPF报文都有哪些,其作用? OSPF报文主要有:hello报文.DD报文.LSR报文.LSU报文和LSAck报文. Hello报文主要用来建立和维护邻居关系. DD报文是链路状态数据库的 ...

  4. Gap year | 最好金龟换酒

    Gap year | 最好金龟换酒 Gap year Posted on February 8, 2009 by 真 后.90后相比,说是虽然形成背景不同,但有很多特征相似,比如离经叛道,比如信仰缺失 ...

  5. Ucan23操作系统项目地址

    期间耽误了近半年的时间.在昨天最终完毕了Ucan23OS, 项目托管在GitHub上,地址为: https://github.com/howardking/UCAN23OS 以下为操作系统的执行截图 ...

  6. poj1094Sorting It All Out

    主题链接: 啊哈哈,选我 题目: Sorting It All Out Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 268 ...

  7. deinstall oracle 11g on linux

    deinstall oracle 11g on linux   From 11gR2, oracle provide us an deinstall tool. With that now we ca ...

  8. Knockout应用开发指南 第七章:Mapping插件

    原文:Knockout应用开发指南 第七章:Mapping插件 Mapping插件 Knockout设计成允许你使用任何JavaScript对象作为view model.必须view model的一些 ...

  9. Exec l 中分列的作用

  10. Google的Guava它Collection升华

    至于Guava这是不是在这里说.一个已被提上一个非常特殊的! 这主要是为了分享Guava对于一些升华处理组.井,不多说了,直接在代码: package com.joyce.guava.bean; /* ...