LeetCode 108. Convert Sorted Array to Binary Search Tree

Posted flowingfog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 108. Convert Sorted Array to Binary Search Tree相关的知识,希望对你有一定的参考价值。

分析

难度 易

来源

https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/

题目

Given an array where elements are sorted in ascending order, convert it to a height balanced BST.

For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1.

Example:

Given the sorted array: [-10,-3,0,5,9], 
One possible answer is: [0,-3,9,-10,null,5], which represents the following height balanced BST: 
      0
     / 
   -3   9
   /   /
 -10  5
解答
 1 package LeetCode;
 2 
 3 import java.util.LinkedList;
 4 import java.util.Queue;
 5 
 6 public class L108_ConvertSortedArray2BinarySearchTree {
 7     public TreeNode makeBinaryTreeByArray(int[] nums,int low,int high){
 8         if(low<=high){
 9             int mid=(high+low)/2;
10             TreeNode root=new TreeNode(nums[mid]);
11             //System.out.println(root.val);
12             root.left=makeBinaryTreeByArray(nums,low,mid-1);
13             root.right=makeBinaryTreeByArray(nums,mid+1,high);
14             return root;
15         }
16         else
17             return null;
18     }
19     public TreeNode sortedArrayToBST(int[] nums) {
20         return makeBinaryTreeByArray(nums,0,nums.length-1);
21     }
22     public void levelOrderTraversal(TreeNode root){//广度优先搜索+分层
23         if(root==null){
24             //System.out.println("empty tree");
25             return;
26         }
27         Queue<TreeNode> queue=new LinkedList<TreeNode>();
28         queue.add(root);
29         int curCount=1;//记录当前层节点数
30         int nextCount=0;//记录下一层节点数
31         int outCount=0;//记录出队列节点数
32         while(!queue.isEmpty()){
33             TreeNode node=queue.remove();
34             outCount++;
35             System.out.print(node.val+"	");
36             if(node.left!=null){
37                 queue.add(node.left);
38                 nextCount++;
39             }
40             if(node.right!=null){
41                 queue.add(node.right);
42                 nextCount++;
43             }
44             if(outCount==curCount)//当前层全部出队列
45             {
46                 System.out.println();
47                 curCount=nextCount;
48                 outCount=0;
49             }
50         }
51         System.out.println("");
52     }
53     public static void main(String[] args){
54         int[] nums={-10,-3,0,5,9};
55         L108_ConvertSortedArray2BinarySearchTree l108=new L108_ConvertSortedArray2BinarySearchTree();
56         TreeNode root=l108.sortedArrayToBST(nums);
57         l108.levelOrderTraversal(root);
58     }
59 }

 

 

以上是关于LeetCode 108. Convert Sorted Array to Binary Search Tree的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 108. Convert Sorted Array to Binary Search Tree

Leetcode 108. Convert Sorted Array to Binary Search Tree

LeetCode_108. Convert Sorted Array to Binary Search Tree

[LeetCode]题解(python):108-Convert Sorted Array to Binary Search Tree

Leetcode 108. Convert Sorted Array to Binary Search Tree

leetcode108. Convert Sorted Array to Binary Search Tree