【Lintcode]177.Convert Sorted Array to Binary Search Tree With Minimal Height

题目:

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height.

Example

Given [1,2,3,4,5,6,7], return

     4
   /     2     6
 / \    / 1   3  5   7

题解:

Solution 1 ()

class Solution {
public:
    TreeNode *sortedArrayToBST(vector<int> &A) {
        if (A.size() == 0) return nullptr;

        return buildTree(A, 0, A.size() - 1);
    }
    TreeNode* buildTree(vector<int>&A, int begin, int end) {
        if (begin > end) {
            return nullptr;
        }
        int mid = begin + (end - begin) / 2;
        TreeNode* root = new TreeNode(A[mid]);
        root->left = buildTree(A, begin, mid - 1);
        root->right = buildTree(A, mid + 1, end);
        return root;
    }
};
时间: 2024-11-07 05:39:19

【Lintcode]177.Convert Sorted Array to Binary Search Tree With Minimal Height的相关文章

[lintcode easy]Convert Sorted Array to Binary Search Tree With Minimal Height

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / 2 6 / \ / 1 3 5 7 Note There may exist multiple valid solutions, return any of them. ////////////////////// 二叉查

Convert Sorted Array to Binary Search Tree With Minimal Height

Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Given [1,2,3,4,5,6,7], return 4 / 2 6 / \ / 1 3 5 7 分析:这是一道非常明显的递归题.取array的中间数作为树的root,array 左边部分是左子树部分,array右边部分是右子树部分. 1 /** 2 * Definition of

LintCode: Convert Sorted Array to Binary Search Tree With Minimal Height

C++ /** * Definition of TreeNode: * class TreeNode { * public: * int val; * TreeNode *left, *right; * TreeNode(int val) { * this->val = val; * this->left = this->right = NULL; * } * } */ class Solution { public: /** * @param A: A sorted (increasi

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

【树】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】【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

【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=(begi

Lintcode177-Convert Sorted Array to Binary Search Tree With Minimal Height-Easy

177. Convert Sorted Array to Binary Search Tree With Minimal Height Given a sorted (increasing order) array, Convert it to create a binary tree with minimal height. Example Example 1: Input: {1,2} Output: A binary search tree with minimal height. Exp

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 *