[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 middle element and create root node N.

2. recursively create a balanced BST from the left half of the array and set its root as N‘s left node.

3. recursively create a balanced BST from the right half of the array and set its root as N‘s right node.

T(n) = 2 * T(n/2) + O(1),  so the time complexity is O(n).

 1 class TreeNode {
 2     TreeNode left;
 3     TreeNode right;
 4     int val;
 5     TreeNode(int val){
 6         this.left = null;
 7         this.right = null;
 8         this.val = val;
 9     }
10 }
11 public class Solution {
12     public TreeNode sortedArrayToBalancedBST(int[] a) {
13         if(a == null || a.length == 0){
14             return null;
15         }
16         return toBSTRecursive(a, 0, a.length - 1);
17     }
18     private TreeNode toBSTRecursive(int[] a, int start, int end){
19         if(start > end){
20             return null;
21         }
22         int mid = start + (end - start) / 2;
23         TreeNode node = new TreeNode(a[mid]);
24         node.left = toBSTRecursive(a, start, mid - 1);
25         node.right = toBSTRecursive(a, mid + 1, end);
26         return node;
27     }
28 }
时间: 2024-12-18 04:23:42

[GeeksForGeeks] Sorted array to balanced BST的相关文章

108. Convert Sorted Array to balanced Binary Search Tree

108. Convert Sorted Array to balanced Binary Search Tree The tricky part is the base case . Write induction part first and then test arrays of different size, 0, 1,2, 3 And finalize the base case /** * Definition for a binary tree node. * public clas

Convert Sorted Array to Balanced Binary Search Tree

题目: Given an array where elements are sorted in ascending order, convert it to a height balanced BST 解答: 1 public class Solution { 2 3 public static void main(String[] args) { 4 int[] num = {1,2,3,4,5}; 5 6 TreeNode root = sortedArrayToBST(num); 7 8

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

Leetcode-Convert Sorted Array to BST

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. Solution: 1 /** 2 * Definition for binary tree 3 * public class TreeNode { 4 * int val; 5 * TreeNode left; 6 * TreeNode right; 7 * TreeNode(int x) { val

leetCode 108.Convert Sorted Array to Binary Search Tree(将排序数组转换为BST) 解题思路和方法

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

Convert Sorted List to Balanced Binary Search Tree (BST)

(http://leetcode.com/2010/11/convert-sorted-list-to-balanced-binary.html) Given a singly linked list where elements are sorted in ascending order, convert it to a height balanced BST. Code: BinaryTree* sortedListToBST(ListNode *& list, int start, int

[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

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