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  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     TreeNode* sortedArrayToBST(vector<int>& nums) {
13         return createTree(nums, 0, nums.size() - 1);
14     }
15
16     TreeNode * createTree(vector<int> & nums, int left, int right)
17     {
18         if(left > right)
19             return NULL;
20         int mid = left + (right - left)/2;
21         TreeNode * leftNode = createTree(nums, left, mid - 1);
22         TreeNode * rightNode = createTree(nums, mid + 1, right);
23         TreeNode * tmpRoot=  new TreeNode(nums[mid]);
24         tmpRoot->left = leftNode;
25         tmpRoot->right = rightNode;
26         return tmpRoot;
27     }
28 };
时间: 2024-12-19 21:05:46

LeetCode OJ:Convert Sorted Array to Binary Search Tree(将排序好的数组转换成二叉搜索树)的相关文章

[Leetcode] Convert sorted list to binary search tree 将排好的链表转成二叉搜索树

---恢复内容开始--- Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 题目要求:转成高度平衡的二叉搜索树. 高度平衡的二叉搜索树:i)左子树和右子树的高度之差的绝对值不超过1; ii)树中的每个左子树和右子树都是AVL树; iii)每个节点都有一个平衡因子(balance factor bf),任一节点的平衡因子是1,0,

leetCode 108.Convert Sorted Array to Binary Search Tree(将排序数组转换为BST) 解题思路和方法

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

[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 *

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

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