LeetCode OJ Convert Sorted Array to Binary Search

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

class Solution {
public:
vector<int> Num;
TreeNode *sortedArrayToBST(vector<int> &num) {
if (num.size() == 0) return NULL;
Num = num;
return make(0, Num.size() - 1);
}
TreeNode * make(int l, int r) {
int mid = (l + r) / 2;
TreeNode * newRoot = new TreeNode(Num[mid]);
if (l < mid) newRoot->left = make(l, mid - 1);
if (mid < r) newRoot->right = make(mid + 1, r);
return newRoot;
}
};
时间: 2024-11-10 01:00:26

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

[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 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][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 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. 题目标签:Tree 这道题目给了我们一个有序数组,从小到大.让我们把这个数组转化为height balanced BST. 首先来看一下什么是binary search tree: 每一个点的left < 节点 < right, 换一句话说,每一个点的值要大于左边的,小于右边的. 那么什么是heigh

LeetCode – Refresh – Convert Sorted Array to Binary Search Tree

It is kind of binary search. Since this is a sorted array, we can treat it as inorder traversal. And we can define the root node for the Tree. So find the middle element as the root, then keep doing recursion. 1 /** 2 * Definition for binary tree 3 *

Java for LeetCode 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. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return

leetcode 108 Convert Sorted Array to Binary Search Tree ----- java

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 给一个排好序的数组,然后求搜索二叉树 其实就是二分法,不难. /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNo

【leetcode】Convert Sorted Array to Binary Search Tree (easy)

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 有序数组变二叉平衡搜索树,不难,递归就行.每次先序建立根节点(取最中间的数),然后用子区间划分左右子树. 一次就AC了 注意:new 结构体的时候对于 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x)

leetcode No108. Convert Sorted Array to Binary Search Tree

Question: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 把有序数组转化成平衡的BST Algorithm: 找到数组中间的元素,作为根节点,则根节点左边是左子树,根节点右边是右子树,接着递归 Accepted Code: /** * Definition for a binary tree node. * struct TreeNode