Topic:
Given an array where elements is sorted in ascending order, convert it to a height balanced BST.
Exercises
And I above an ordered list to binary sorting tree with the solution of the hash table is the same, the basic idea: the middle node of the list is the root node of the tree, the root node of the left subtree node should be the root node on the left side of the middle node, the right node of the root node should be the root node to the right part of the list Followed by this rule and so on.
public static TreeNode Sortedarraytobst (int[] nums) {int end=nums.length; if (end<=0) return null; return Buildtree (num S, 0, end-1);//Because counting starts from 0, minus one } public static TreeNode Buildtree (int[] nums,int Start,int end) {if (start<=end) {in T mid= (start+end)/2; TreeNode root=new TreeNode (Nums[mid]); Root.left=buildtree (Nums, start, mid-1); Root.right=buildtree (Nums, mid+1, end); return root; } else {return null;} }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode108_convert Sortedarray to Binarysearchtree (turn ordered array into binary sort tree) Java