字节跳动面试210615大数据java(搜索二叉树后续遍历)
Posted 史上最强的弟子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了字节跳动面试210615大数据java(搜索二叉树后续遍历)相关的知识,希望对你有一定的参考价值。
面试的是一位大数据组的老大,上来就让先写一个搜索二叉树后续遍历,半小时写出来。
因为当时太紧张没写出来后面捋了一下思路写出来了。这个其实很考虑思想的转变过程,和大家分享一下解法,经过检验100% 能输出,原创努力来之不易。继续努力。
package jieduaner;
import java.util.Arrays;
import java.util.List;
public class ErTreeFind {
//给数组构成搜索二叉树 后续遍历, 搜索二叉树特点,1.根节点最多有两个子节点,2.根节点值大于左节点的值,根节点的值小于右节点的值。
public static void main(String[] args) {
//[123 576 4]
/* 4
2 6
1 3 5 7
*/
ErTreeFind erTreeFind = new ErTreeFind();
Integer[] datas = new Integer[]{1,3,2,5,7,6,4};
List<Integer> dataList = Arrays.asList(datas);
ErTreeNode erTreeNode = erTreeFind.getNode(null,dataList);
System.out.println(erTreeNode);
/* 1
2
3
4
5*/
datas = new Integer[]{5,4,3,2,1};
dataList = Arrays.asList(datas);
erTreeNode = erTreeFind.getNode(null,dataList);
System.out.println(erTreeNode);
/* 5
4
3
2
1 */
datas = new Integer[]{1,2,3,4,5};
dataList = Arrays.asList(datas);
erTreeNode = erTreeFind.getNode(null,dataList);
System.out.println(erTreeNode);
}
public ErTreeNode getNode(ErTreeNode erTreeNode,List<Integer> dataList){
ErTreeNode returnNode = null;
//边界
if(dataList==null||dataList.size()== 0){
return null;
}
ErTreeNode rootNode = new ErTreeNode();
rootNode.value = dataList.get(dataList.size()-1);
List<Integer> subDataList = dataList.subList(0,dataList.size()-1);
if(erTreeNode == null){
returnNode = rootNode;
}
if(erTreeNode!= null){
if(erTreeNode.value>rootNode.value){
erTreeNode.leftNode = rootNode;
}else{
erTreeNode.rightNode = rootNode;
}
}
int remark = -1;
for(int i = subDataList.size()-1;i>=0;i--){
int value = subDataList.get(i);
if(value<rootNode.value){
remark = i;
break;
}
}
//右树
getNode(rootNode,subDataList.subList(remark==-1?0:remark+1, subDataList.size()));
//左树
if(remark>=0) {
getNode(rootNode, subDataList.subList(0, remark+1));
}
return returnNode;
}
}
class ErTreeNode{
public ErTreeNode leftNode;
public ErTreeNode rightNode;
public Integer value;
@Override
public String toString() {
return "ErTreeNode{" +
"leftNode=" + leftNode +
", rightNode=" + rightNode +
", value=" + value +
'}';
}
}
输出结果:
//测试1输出
ErTreeNode{leftNode=ErTreeNode{leftNode=ErTreeNode{leftNode=null, rightNode=null, value=1}, rightNode=ErTreeNode{leftNode=null, rightNode=null, value=3}, value=2}, rightNode=ErTreeNode{leftNode=ErTreeNode{leftNode=null, rightNode=null, value=5}, rightNode=ErTreeNode{leftNode=null, rightNode=null, value=7}, value=6}, value=4}
//测试2输出
ErTreeNode{leftNode=null, rightNode=ErTreeNode{leftNode=null, rightNode=ErTreeNode{leftNode=null, rightNode=ErTreeNode{leftNode=null, rightNode=ErTreeNode{leftNode=null, rightNode=null, value=5}, value=4}, value=3}, value=2}, value=1}
//测试3输出
ErTreeNode{leftNode=ErTreeNode{leftNode=ErTreeNode{leftNode=ErTreeNode{leftNode=ErTreeNode{leftNode=null, rightNode=null, value=1}, rightNode=null, value=2}, rightNode=null, value=3}, rightNode=null, value=4}, rightNode=null, value=5}
以上是关于字节跳动面试210615大数据java(搜索二叉树后续遍历)的主要内容,如果未能解决你的问题,请参考以下文章
字节跳动面试210615大数据java(搜索二叉树后续遍历,非递归)
字节跳动面试210615大数据java(搜索二叉树后续遍历,非递归)