zoukankan      html  css  js  c++  java
  • 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 }

     

    博客园的编辑器没有CSDN的编辑器高大上啊
  • 相关阅读:
    回调函数
    BIRT实现组内跨行计算
    POJ 3616 Milking Time DP题解
    string的内存管理问题
    天津政府应急系统之GIS一张图(arcgis api for flex)解说(三)显示地图坐标系模块
    myeclipse将java项目转换成web项目,导出war包
    Spring之IOC篇章具体解释
    为RAC私有网络配置网卡Bonding
    聊聊高并发(九)实现几种自旋锁(四)
    鼠标放上去Div旋转特效代码
  • 原文地址:https://www.cnblogs.com/flowingfog/p/9903233.html
Copyright © 2011-2022 走看看