Convert Sorted Array to Binary Search Tree--LeetCode

题目:

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

思路:从一个数组中找到中间的元素作为BST的根,然后坐边的作为左子树,右边的作为右子树,递归调用

#include <iostream>
#include <stack>
#include <vector>
#include <deque>
#include <stdlib.h>
using namespace std;

typedef struct Bin_tree BinTree;
struct Bin_tree
{
       int value;
       BinTree* right;
       BinTree* left;
};
BinTree* helper(vector<int>& vec,int low,int high)
{
	BinTree* node=NULL;
	if(low > high)
		return NULL;
	int mid = low+(high-low)/2;
	node = new BinTree;
	node->value = vec[mid];
	node->left = NULL;
	node->right = NULL;
	node->left = helper(vec,low,mid-1);
	node->right = helper(vec,mid+1,high);
	return node;
}

BinTree* ConvertBST(vector<int>& vec)
{
	BinTree* root=NULL;
	if(vec.size() <=0)
		return root;
	root = helper(vec,0,vec.size()-1);
}
void Inorder(BinTree* root)
{
     if(root == NULL)
       return ;
     Inorder(root->left);
     cout<<root->value<<endl;
     Inorder(root->right);
     }

int main()
<span style="font-family:'Helvetica Neue',arial,sans-serif;">{</span>	<span style="font-family:'Helvetica Neue',arial,sans-serif;"> </span>
    BinTree* second=NULL;
  <span style="font-family:'Helvetica Neue',arial,sans-serif;"> </span>
    int sec[]={4,5,6,10,14,16};
    vector<int> vec(sec,sec+sizeof(sec)/sizeof(int));
    second = ConvertBST(vec);
	Inorder(second);
	return 0;
}
时间: 2024-10-14 10:25:50

Convert Sorted Array to Binary Search Tree--LeetCode的相关文章

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),是指一棵空树或者具有下列性质的二叉树: 若任意节点的左子树不空,则左子树

Convert Sorted Array to Binary Search Tree——LeetCode

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 题目大意:给定一个升序序列的数组,将其转换为二叉搜索树. 解题思路:数组中间元素是根元素,根元素将数组划分为两部分,两个部分的中心元素分别为根元素的左右孩子,依次递推...可用迭代或递归来做. 解法一(迭代):迭代需要自己额外记录下标 public TreeNode sortedArrayToBST(i

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

[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 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][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,

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:

Convert Sorted List to Binary Search Tree leetcode java

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题解: 之前做过一道是从sorted array转换到BinarySearchTree的,方法还是一样二分法.但是构造树的方法不是由顶至下了,是要由低至上的建立. 代码如下: 1     static ListNode h; 2   3     public TreeNo