Topic:
Given an array where elements is sorted in ascending order, convert it to a height balanced BST.
Exercises
And I have the same method of turning the ordered list into a binary sort tree using a hash table solution. Basic idea: The node in the middle of the list is the root node of the tree. The left subtree node of the root node should be the middle node of the left part of the root node, and the right node of the root node should be the middle node of the list on the right side of the root node. The following is followed by this law.
public static TreeNode Sortedarraytobst (int[] nums) {int end=nums.length; if (end<=0) return null; return Buildtree (num S, 0, end-1);//due to start counting from 0 so 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;} }
Leetcode108_convert Sortedarray to Binarysearchtree (turn ordered array into binary sort tree) Java