朗致集团面试总结

不知道小伙伴们最近有没有面试朗致集团的,分享一下他家的面试题,让大家有个准备。

提问环节很简单,就问了双向链表的概念和满二叉树的概念。

然后是写代码环节,基本上会出三道题,每道题5分钟左右。

第一道题是实现链表,然后逐渐演变成二叉树,最后给二叉树赋值,顺序是从上到下,从左到右。我当时写的时候有一处写错了,时间紧迫实在没及时找到问题所在。

面试结束后,总结整理代码如下,希望能帮到大家:

import java.util.LinkedList;

import java.util.Queue;



public class LinkedTable<T> {

private T value;

private LinkedTable father;

private LinkedTable leftChild;

private LinkedTable rightChild;



public T getValue() {

return value;

}



public void setValue(T value) {

this.value = value;

}



public LinkedTable getFather() {

return father;

}



public LinkedTable setFather(LinkedTable father) {

this.father = father;

return this;

}



public LinkedTable getLeftChild() {

return leftChild;

}



public void setLeftChild(LinkedTable leftChild) {

this.leftChild = leftChild;

}



public LinkedTable getRightChild() {

return rightChild;

}



public void setRightChild(LinkedTable rightChild) {

this.rightChild = rightChild;

}



public static LinkedTable createFullTree(int depth) {

if (depth <= 0) {

return null;

}

LinkedTable linkedTable = new LinkedTable();

// todo, set CHIldren

setChildren(linkedTable, depth - 1);

return linkedTable;

}



public static void setValue(LinkedTable node, int[] value) {

Queue<LinkedTable> queue = new LinkedList<>();

queue.offer(node);

int i = 0;

while (!queue.isEmpty() && i < value.length) {

LinkedTable currentNode = queue.poll();

currentNode.setValue(value[i++]);

if (currentNode.leftChild != null) {

queue.offer(currentNode.leftChild);

}

if (currentNode.rightChild != null) {

queue.offer(currentNode.rightChild);

}

}

}



public static void setChildren(LinkedTable father, int depth) {

if (depth > 0) {

father.leftChild = new LinkedTable().setFather(father);

father.rightChild = new LinkedTable().setFather(father);

setChildren(father.leftChild, depth - 1);

setChildren(father.rightChild, depth - 1);

}

}



public static void main(String[] args) {

int[] arr = new int[15];

for (int i = 0; i < arr.length; i++) {

arr[i] = i + 1;

}

LinkedTable fullTree = createFullTree(3);

setValue(fullTree, arr);

System.out.println(fullTree);

}

}

最后祝大家面试顺利。

  • 17
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值