zoukankan      html  css  js  c++  java
  • 《程序员代码面试指南》第三章 二叉树问题 通过有序数组生成搜索二叉树

    题目

    通过有序数组生成搜索二叉树
    

    java代码

    package com.lizhouwei.chapter3;
    
    /**
     * @Description:通过有序数组生成搜索二叉树
     * @Author: lizhouwei
     * @CreateDate: 2018/4/15 10:14
     * @Modify by:
     * @ModifyDate:
     */
    public class Chapter3_16 {
        //生成树
        public Node generTree(int[] arr, int left, int right) {
            if (left > right) {
                return null;
            }
            int min = (left + right) / 2;
            Node head = new Node(arr[min]);
            head.left = generTree(arr, left, min - 1);
            head.right = generTree(arr, min + 1, right);
            return head;
        }
    
        //测试
        public static void main(String[] args) {
            Chapter3_16 chapter = new Chapter3_16();
            int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
            chapter.generTree(arr, 0, arr.length - 1);
        }
    }
    
    
  • 相关阅读:
    UVa10779
    UVa10779
    C++ 内存管理学习笔记
    c++ 学习笔记
    AcWing 275 传纸条
    I
    Tree HDU6228
    Lpl and Energy-saving Lamps
    C
    Secret Poems
  • 原文地址:https://www.cnblogs.com/lizhouwei/p/8845976.html
Copyright © 2011-2022 走看看