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

采用二分递归。

 1 class Solution {
 2 public:
 3     TreeNode *createTree(vector<int> &num,int begin,int end)
 4     {
 5         if(begin>end) return NULL;
 6         int mid=(begin+end)/2;
 7         TreeNode *root=new TreeNode(num[mid]);
 8         root->left=createTree(num,begin,mid-1);
 9         root->right=createTree(num,mid+1,end);
10         return root;
11     }
12
13     TreeNode *sortedArrayToBST(vector<int> &num) {
14         if(num.size()==0) return NULL;
15         return createTree(num,0,num.size()-1);
16     }
17 };
时间: 2024-12-18 07:37:46

【leetcode】Convert Sorted Array to Binary Search Tree的相关文章

【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】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. [解析] 分治:用快慢指针找到链表的中点,作为树的root,然后二分--中点之前的链表和中点之后的链表分别再构造二叉平衡树. /** * Definition for singly-linked list. * public class ListNode { * int

【Leetcode】【Medium】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,可以通过设置左子树结点数等于或者比右子树结点数多1,来实现. 那么每次取数组的中间位置后一个值,作为根结点,数组左边元素的插入左子树,数组右边元素插入右子树,依次类推. 代码: 1 /** 2 * Defi

【树】Convert Sorted Array to Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 思路: 找到数组的中间数据作为根节点,小于中间数据的数组来构造作为左子树,大于中间数据的数组来构造右子树. /** * Definition for a binary tree node. * function TreeNode(val) { * this.val = val; * this.l

【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. 解答 题目要求将链表转化为二叉查找树.利用树的中序遍历的递归思想,对链表结点一个个进行访问,先对左子树进行递归,然后将当前结点作为根,迭代到下一个链表结点,最后在递归求出右子树即可 /** * Definition for singly-linked list. * pub

【leetcode】Convert Sorted List to Binary Search Tree (middle)

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. 思路:题目看上去好像很难,但实际上很简单,递归做就行,每次找到左右子树对应的子链表就行.一次AC. class Solution { public: TreeNode *sortedListToBST(ListNode *head) { if(head == NULL) retu

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

[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