[C++]LeetCode: 100 Convert Sorted Array to Binary Search Tree (AVL树)

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

思路:给出一个排序的数组,如何构造一个平衡二叉查找树?平衡二叉查找树要求任一结点的左右子树的高度差不能超过一,也叫做高度平衡树。如果让我们从一个排序数组中选取一个元素做树的根,我们会选择哪一个树呢?凭直觉,我们觉得应该选择数组的中间值做根,事实也是这样,平衡二叉查找树(AVL树)的根应该是排序数组的中间元素。(我们把排序数组的中间节点作为根,可保证左右子树的元素个数差不超过1,则肯定是平衡二叉树。但并不是唯一的AVL树,不过工程中这种选择中间节点做根的查找树很常用,方便二分查找。)我们用这个元素创建树的根结点,接下来剩下左右两部分的数组,继续选择中间元素,进行递归,左子树的根应该是左部分数组的中间元素,同理右子树的根也是右部分数组的中间元素。利用了分而治之的方法,不断解决子问题。

Attention:

1. 递归终止条件,start > end, 返回NULL. 也包含了数组为空的情况(start = 0, end = -1)。

AC Code:

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &num) {
        return sortedArrayToBST_helper(num, 0, num.size()-1);
    }

private:
    TreeNode* sortedArrayToBST_helper(vector<int> &num, int start, int end)
    {
        if(start > end)
            return NULL;
        int mid = start + (end - start)/2;
        TreeNode* root = new TreeNode(num[mid]);
        root->left = sortedArrayToBST_helper(num, start, mid-1);
        root->right = sortedArrayToBST_helper(num, mid+1, end);
        return root;
    }
};

时间: 2024-10-05 16:39:27

[C++]LeetCode: 100 Convert Sorted Array to Binary Search Tree (AVL树)的相关文章

[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