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

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
      public TreeNode sortedArrayToBST(int[] num) {
                if(num==null||num.length==0){
            return null;
        }
        int length=num.length;
        TreeNode treenode=sortedArrayToBST(num,0,length-1);//我每次总是忘记减一,以后要记住
        return treenode ;

    }

    private TreeNode sortedArrayToBST(int[] num, int star, int tail) {
          if(star>tail){
            return null;
        }
        int mid=star+(tail-star+1)/2;
        TreeNode root=new TreeNode(num[mid]);
        root.left=sortedArrayToBST(num,star,mid-1);
        root.right=sortedArrayToBST(num,mid+1,tail);
        return root;
    }
}
时间: 2024-10-03 06:17:57

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.的相关文章

Convert Sorted List to Balanced BST

Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Example 2 1->2->3 => / 1 3 分析:非常简单,用递归即可.但是如果list的size小于3,就直接创建tree.大于3才递归,否则递归会出错.并且,需要注意返回mid node的时候,要把整个list分成两半. 1 /** 2 * Definiti

【Lintcode】106.Convert Sorted List to Balanced BST

题目: Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Example 2 1->2->3 => / 1 3   题解: Solution 1 () class Solution { public: TreeNode *sortedListToBST(ListNode *head) { if (!head) retur

[GeeksForGeeks] Sorted array to balanced BST

Given a sorted array. Write a program that creates a Balanced Binary Search Tree using array elements. If there are n elements in array, then floor(n/2)'th element should be chosen as root and same should be followed recursively. Solution. 1. get the

usage of char array which elements are mutilple STRINGs ended with a "\0"

usage of char array which elements are mutilple STRINGs ended with a "\0". #include <stdio.h> char stringsarray[] = {         "many" "strings" "\0"         "are" "stored" "\0"    

Search in Rotated Sorted Array &amp;&amp; Search in Rotated Sorted ArrayII

Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). You are given a target value to search. If found in the array return its index, otherwise retu

Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码: class Solution { public: int search(vector<int>& nums, int target) { int numsSize = nums.size(); int low = 0,high = numsSize; while(low < hi

Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. public TreeNode sortedArrayToBST(int[] num) { if (num.length == 0) { return null; } TreeNode head = helper(num, 0, num.length - 1); return head; } publi

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