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         InOrder(root);
 9
10     }
11
12     public static TreeNode sortedArrayToBST(int[] num) {
13         return sortedArrayToBST(num, 0, num.length-1);
14     }
15
16     private static sortedArrayToBST(int[] arr, int start, int end) {
17         if(start > end) {
18             return null;
19         }
20
21         int mid = (start + end) >> 1;
22         TreeNode node = new TreeNode(arr[mid]);
23         node.left = sortedArrayToBST(arr, start, mid-1);
24         node.right = sortedArrayToBST(arr, mid+1, end);
25         return node;
26     }
27
28     public static void InOrder(TreeNode root) {
29
30         if(root == null) {
31             return;
32         }
33
34         if(root.left != null) {
35             InOrder(root.left);
36         }
37
38         System.out.println(root.val);
39
40         if(root.right != null) {
41             InOrder(root.right);
42         }
43
44     }
45 }

原文地址:https://www.cnblogs.com/wylwyl/p/10419085.html

时间: 2024-11-08 08:14:22

Convert Sorted Array to Balanced Binary Search Tree的相关文章

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

LeetCode OJ - Convert Sorted Array/List to Binary Search Tree

虚函数使用的时机 为什么虚函数不总是适用? 1. 虚函数有事会带来很大的消耗: 2. 虚函数不总是提供所需的行为: 3. 当我们不考虑继承当前类时,不必使用虚函数. 必须使用虚函数的情况: 1. 当你想删除一个表面上指向基类对象,实际却是指向派生类对象的指针,就需要虚析构函数. LeetCode OJ - Convert Sorted Array/List to Binary Search Tree,布布扣,bubuko.com LeetCode OJ - Convert Sorted Arra

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

Convert Sorted List to Balanced Binary Search Tree leetcode

题目:将非递减有序的链表转化为平衡二叉查找树! 参考的博客:http://blog.csdn.net/worldwindjp/article/details/39722643 利用递归思想:首先找到链表的中间节点,于是链表被分为了由该中间节点划分开来的两部分.递归地处理这两部分,最终便得到了平衡二叉查找树. 1 /** 2 * Definition for singly-linked list. 3 * struct ListNode { 4 * int val; 5 * ListNode *n

[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

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 & 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 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