转载请标明出处http://www.cnblogs.com/haozhengfei/p/2490d09c73b23343eaa5ccaa090bf409.html
数组变树练习题
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132813713-1823781999.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132813995-1018934663.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132814260-707239478.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132814604-174001869.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132814932-1662248476.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132815260-1779957422.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132815573-1008447770.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132815854-543429745.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132816151-1892031634.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132816416-77639707.jpg)
![](https://images2015.cnblogs.com/blog/1008304/201703/1008304-20170319132816885-1040884540.jpg)
数组变树练习
第9节 数组变树练习题
对于一个没有重复元素的整数数组,请用其中元素构造一棵MaxTree,MaxTree定义为一棵二叉树,其中的节点与数组元素一一对应,同时对于MaxTree的每棵子树,它的根的元素值为子树的最大值。现有一建树方法,对于数组中的每个元素,其在树中的父亲为数组中它左边比它大的第一个数和右边比它大的第一个数中更小的一个。若两边都不存在比它大的数,那么它就是树根。请设计O(n)的算法实现这个方法。
给定一个无重复元素的数组A和它的大小n,请返回一个数组,其中每个元素为原数组中对应位置元素在树中的父亲节点的编号,若为根则值为-1。
测试样例:
[3,1,4,2],4
返回:[2,0,-1,2]
您的代码已保存
答案正确:恭喜!您提交的程序通过了所有的测试用例
答案正确:恭喜!您提交的程序通过了所有的测试用例