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

//////////////////////

二叉查找树Binary Search Tree),也称有序二叉树(ordered binary tree),排序二叉树(sorted binary tree),是指一棵空树或者具有下列性质的二叉树

  1. 若任意节点的左子树不空,则左子树上所有结点的值均小于它的根结点的值;
  2. 任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
  3. 任意节点的左、右子树也分别为二叉查找树。

平衡二叉树(引自GeekforGeek):

An empty tree is height-balanced. A non-empty binary tree T is balanced if:
   1) Left subtree of T is balanced
   2) Right subtree of T is balanced
   3) The difference between heights of left subtree and right subtree is not more than 1.

////////////////////////////////////////////////////

/**
 * Definition of TreeNode:
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left, right;
 *     public TreeNode(int val) {
 *         this.val = val;
 *         this.left = this.right = null;
 *     }
 * }
 */
public class Solution {
    /**
     * @param A: an integer array
     * @return: a tree node
     */
    public TreeNode sortedArrayToBST(int[] A) {
        // write your code here
        if(A.length==0) return null;

        return buildTree(A,0,A.length-1);
    }  

    public TreeNode buildTree(int[] A, int low, int high)
    {
        if(low>high) return null;
        int mid=(low+high)/2;
        TreeNode node=new TreeNode(A[mid]);
        node.left=buildTree(A,low,mid-1);
        node.right=buildTree(A,mid+1,high);

        return node;

    }
}
时间: 2024-11-10 07:04:03

[lintcode easy]Convert Sorted Array to Binary Search Tree With Minimal Height的相关文章

【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

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

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_108. Convert Sorted Array to Binary Search Tree

fasffasfa 108. 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. For this problem, a height-balanced binary tree is defined as a binary tree in which the

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

39.1: Convert Sorted Array to Binary Search Tree

/************************************************************************/            /*       39.1:  Convert Sorted Array to Binary Search Tree                               */            /************************************************************

Convert Sorted Array to Binary Search Tree &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. 方法:将有序数组转为平衡二叉树,我们知道平衡二叉树的中序遍历是有序数组,而且“中间”的元素可以作为根节点,那么构建树的时候,找出中间元素,构造根元素,然后继续重复上面的方法,构造左子树和右子树.代码如下: 1 /**

[leetcode]Convert Sorted Array to Binary Search Tree @ Python

原题地址:http://oj.leetcode.com/problems/convert-sorted-array-to-binary-search-tree/ 题意:将一个排序好的数组转换为一颗二叉查找树,这颗二叉查找树要求是平衡的. 解题思路:由于要求二叉查找树是平衡的.所以我们可以选在数组的中间那个数当树根root,然后这个数左边的数组为左子树,右边的数组为右子树,分别递归产生左右子树就可以了. 代码: # Definition for a binary tree node # class