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

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.