leetcode || 108、Convert Sorted Array to Binary Search Tree

problem:

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

Hide Tags

Tree Depth-first
Search

题意:将一个递增的序列 转换成一棵 平衡查找二叉树

thinking:

(1)平衡二叉树的概念:左右子树的高度差最大不能超过1,查找二叉树的概念:左孩子<父节点<右孩子

(2)二分法递归构造二叉树

(3)模板函数解决形参类型vector<int>::iterator 太长,书写不方便

code:

class Solution {
  public:
      TreeNode *sortedArrayToBST(vector<int> &num) {
          if(num.size()==0)
              return NULL;
          return make(num.begin(),num.end());

      }
      template<class it>
      TreeNode *make(it first,it last)
      {
          if(first==last)
              return NULL;
          it loc = first+(last-first)/2;
          TreeNode *node = new TreeNode(*loc);
          node->left=make(first,loc);
          node->right=make(loc+1,last);
          return node;
      }
  };
时间: 2024-11-03 03:41:32

leetcode || 108、Convert Sorted Array to Binary Search Tree的相关文章

LeetCode OJ: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 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * T

leetcode || 109、Convert Sorted List to Binary Search Tree

problem: 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 题意:给定一个递增的单链表,将其转换成平衡查找二叉树,这道题是上道题的变形 thinking: (1)平衡二叉树的概念:左右子树的高度差最大不能超过1,查找二叉树的概念:左孩子<父

LeetCode OJ 108. Convert Sorted Array to Binary Search Tree DFS求解

题目链接:https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 108. Convert Sorted Array to Binary Search Tree My Submissions Question Total Accepted: 68378 Total Submissions: 187560 Difficulty: Medium Given an array where elements ar

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 *

leetcode 108 Convert Sorted Array to Binary Search Tree

题目连接 https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ Convert Sorted Array to Binary Search Tree Description Given an array where elements are sorted in ascending order, convert it to a height balanced BST. /** * Definition f

Convert Sorted Array to Binary Search Tree leetcode java

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 先复习下什么是二叉搜索树(引自Wikipedia): 二叉查找树(Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary 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][BST][Convert Sorted Array to Binary Search Tree]

把一个排好序的vector转换为一颗二分查找树. 很简单的题目,递归即可,保证边界不要出错. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 1

[Leetcode][JAVA] Convert Sorted Array to Binary Search Tree &amp;&amp; Convert Sorted List 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. 很简单的二分法,只要给出Array的开始和结束下标作为参数传入即可. 1 public TreeNode sortedArrayToBST(int[] num) { 2 return constructBST(num,