Leetcode108_convert Sortedarray to Binarysearchtree (turn ordered array into binary sort tree) Java

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.