【IT笔试面试题整理】有序数组生成最小高度二叉树
【试题描述】定义一个函数,输入一个有序数组生成最小高度二叉树
We will try to create a binary tree such that for each node, the number of nodes in the left
subtree and the right subtree are equal, if possible
Algorithm:
1 Insert into the tree the middle element of the array
2 Insert (into the left subtree) the left subarray elements
3 Insert (into the right subtree) the right subarray elements
4 Recurse
【参考代码】
1 public static Node addToTree(int[] arr,int start,int end)
2 {
3 if(end < start)
4 return null;
5 int mid = (start + end)/2;
6 Node n = new Node(arr[mid]);
7 n.left = addToTree(arr,start,mid-1);
8 n.right = addToTree(arr,mid+1,end);
9 return n;
10 }
11 public static Node createMinBST(int[] array)
12 {
13 return addToTree(array,0,array.length - 1);
14 }
【IT笔试面试题整理】有序数组生成最小高度二叉树的更多相关文章
- 【IT笔试面试题整理】数组中出现次数超过一半的数字
[试题描述]数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字. [试题分析]时间复杂度O(n),空间复杂度O(1) 思路1: 创建一个hash_map,key为数组中的数,value为此数 ...
- Java笔试面试题整理第四波
转载至:http://blog.csdn.net/shakespeare001/article/details/51274685 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第三波
转载至:http://blog.csdn.net/shakespeare001/article/details/51247785 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第八波
转载至:http://blog.csdn.net/shakespeare001/article/details/51388516 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第六波(修正版)
转载至:http://blog.csdn.net/shakespeare001/article/details/51330745 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第五波
转载至:http://blog.csdn.net/shakespeare001/article/details/51321498 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第一波
转载至:http://blog.csdn.net/shakespeare001/article/details/51151650 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- Java笔试面试题整理第二波
转载至:http://blog.csdn.net/shakespeare001/article/details/51200163 作者:山代王(开心阳) 本系列整理Java相关的笔试面试知识点,其他几 ...
- 【剑指offer】面试题 11. 旋转数组的最小数字
面试题 11. 旋转数组的最小数字 题目描述 题目:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
随机推荐
- 学习protobuf 感想
前俩篇博文是从大牛的博客抄过来的, 写的都很好. 这里还写简单写下自己的感想: 1. 和json比, protobuff编码后的体积小很多, 这是肯定的. 都源自于protobuff内部的一系列特殊的 ...
- android sqlite select count
用sqlite 查数据多次都没有成功,原来是sql语句不对. 正确的示例如下: select count(*) as mycount from kehu
- hdu 5058 set应用
http://acm.hdu.edu.cn/showproblem.php?pid=5058 set应用 水题 #include <cstdio> #include <cstdlib ...
- ES版本控制
版本控制 ElasticSearch采用了乐观锁来保证数据的一致性,也就是说,当用户对document进行操作时,并不需要对该doucument作加锁和解锁的操作,只需要指定要操作的版本即可.当版本号 ...
- node API
看了这么久nodejs,它包含哪些基础的API,自己心中都没有数,该完整地看看基础的API了. 见过的: assert:编写程序的单元测试用例 buffer:直接处理2进制数据 child_proce ...
- ElementTriArgyris
class ElementTriArgyris(ElementH2): nodal_dofs = 6 facet_dofs = 1 dim = 2 maxdeg = 5 def gdof(self, ...
- ios 百度地图,火星坐标,地球坐标互转
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- [Leet code 2]Two Sum
1 题目 You are given two linked lists representing two non-negative numbers. The digits are stored in ...
- golang 本地构建包
说点废话 为小胖做一个温馨提示的功能,思路已经整理好.今天使用goquery的时候,发现已经修改了.其中需要一个golang.org/x/net/html这个包,但是官网已经被万恶的GFW给墙了.这里 ...
- Kali Linux渗透测试实战 1.1 Kali Linux简介
1.1 Kali Linux简介 如果您之前使用过或者了解BackTrack系列Linux的话,那么我只需要简单的说,Kali是BackTrack的升级换代产品,从Kali开始,BackTrack将成 ...