朗致面试过程

文章讲述了作者在求职过程中遭遇的面试经历,包括对双向链表和满二叉树概念的理解以及如何在10分钟内尝试实现一个满二叉树的Node类和创建过程。
摘要由CSDN通过智能技术生成

年底因为遇到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);
    }
}

  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值