年底因为遇到s 。 b甲方主管,被干掉了,不太好找工作,好不容易有一个面试,还挂了,整理一下供大家参考
面试官:什么是双向链表
我:集合中相邻两个node记录了彼此对象地址的集合
面试官:什么是满二叉树
我:二叉树的每一层的节点都有左右子树(除了最下边一层),每一层的节点数为2的n-1次方(n为层数)
面试官:实现一个满二叉树,每一个node记录他的上级以及下级节点地址,node的值需要为NULL,时间为10min(包含思考时间),
我:
定义node类型:
public class Node<T> {
T i;
Node<T> leftChild;
Node<T> rightChild;
Node<T> father;
public T getI() {
return i;
}
public void setI(T i) {
this.i = i;
}
public Node<T> getLeftChild() {
return leftChild;
}
public void setLeftChild(Node<T> leftChild) {
this.leftChild = leftChild;
}
public Node<T> getRightChild() {
return rightChild;
}
public void setRightChild(Node<T> rightChild) {
this.rightChild = rightChild;
}
public Node<T> getFather() {
return father;
}
public void setFather(Node<T> father) {
this.father = father;
}
}
实现:
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class Tree {
static Queue<Node<String>> queue = new ConcurrentLinkedQueue<>();
public static void creatTree(int n){
//int i=2;
//for(int i = 2;i<=n;i++){
while(queue.size()>0){
Node<String> node1 = queue.poll();
//System.out.println(node1.getI());
Node<String> l = new Node<String>();
//l.setI(i+"l");
l.setFather(node1);
node1.setLeftChild(l);
queue.add(l);
Node<String> r = new Node<String>();
//r.setI(i+"r");
r.setFather(node1);
node1.setRightChild(r);
queue.add(r);
//i++;
//System.out.println(n+"------"+Math.pow(2,n-1)+"-----------------"+queue.size());
if(queue.size()==Math.pow(2,n-1)){
break;
}
}
//}
}
public static void main(String[] args) {
Node<String> root = new Node<String>();
//root.setI("1");
queue.add(root);
creatTree(10);
}
}