剑指offer面试题 java解答56-60

面试题56:链表中环的入口结点

public class Test56 {
    private class Node<T> {
        private T obj;
        private Node<T> next = null;
        Node(T obj) {
            this.obj = obj;
        }
    }
    private Node first = null;
    public Node getHeadNode() {
        return first;
    }
    public <T> Node insertTail(T value) {
        Node nextNode=null;
        if (first == null) {
            first = new Node(value);
            return first;
        } else {
            nextNode = first;
            while (nextNode.next != null) {
                nextNode = nextNode.next;
            }
            nextNode.next = new Node(value);
        }
        return nextNode.next;
    }
    private Node MeetingNode(Node head){
        if (head==null) {
            return null;
        }
        Node pSlow=head.next;
        if (pSlow==null) {
            return null;
        }
        Node pFast=pSlow.next;
        while (pSlow!=null&&pFast!=null) {
            if (pSlow==pFast) {
                return pSlow;
            }
            pSlow=pSlow.next;
            pFast=pFast.next;
            if (pFast!=null) {
                pFast=pFast.next;
            }
        }
        return null;
    }
    public Node EntryNodeOfLoop(Node head){
        Node meetingNode=MeetingNode(head);
        if (meetingNode==null) {
            return null;
        }
        int nodesInLoop=1;
        Node pNode1=meetingNode;
        while (pNode1.next!=meetingNode) {
            pNode1=pNode1.next;
            nodesInLoop++;
        }
        pNode1=head;
        for (int i = 0; i < nodesInLoop; i++) {
            pNode1=pNode1.next;
        }
        Node pNode2=head;
        while (pNode1!=pNode2) {
            pNode1=pNode1.next;
            pNode2=pNode2.next;
        }
        return pNode1;
    }
    public static void main(String[] args) {
        Object[] a = { 1, 2, 3, 4, 5, 6 };
        Test56 t = new Test56();
        Node node = null;
        for (int i = 0; i < a.length; i++) {
            if (i == 2) {
                node = t.insertTail(a[i]);
            } else if (i == 5) {
                Node tailNode = t.insertTail(a[i]);
                tailNode.next = node;
            }else {
                t.insertTail(a[i]);
            }
        }
        Node entryNode=t.EntryNodeOfLoop(t.getHeadNode());
        System.out.println(entryNode.obj);
    }
}

面试题57:删除链表中重复的结点

public class Test57 {
    private class Node<T> {
        private T obj;
        private Node<T> next = null;
        Node(T obj) {
            this.obj = obj;
        }
    }
    private Node first = null;
    public Node getHeadNode() {
        return first;
    }
    public <T> void insertTail(T value) {
        Node nextNode=null;
        if (first == null) {
            first = new Node(value);
        } else {
            nextNode = first;
            while (nextNode.next != null) {
                nextNode = nextNode.next;
            }
            nextNode.next = new Node(value);
        }
    }
    public <T> void  deleteDuplication(Node<T> pHead){
        if (pHead==null) {
            return;
        }
        Node preNode=null;
        Node pNode=pHead;
        while (pNode!=null) {
            Node pNext=pNode.next;
            boolean needDelete=false;
            if (pNext!=null&&pNode.obj==pNext .obj) {
                needDelete=true;
            }
            if (!needDelete) {
                preNode=pNode;
                pNode=pNode.next;
            }else {
                T value=(T) pNode.obj;
                Node pToBeDel=pNode;
                while (pToBeDel!=null&&pToBeDel.obj==value) {
                    pNext=pToBeDel.next;
                    pToBeDel=pNext;
                }
                if (preNode==null) {
                    pHead=pNext;
                }else {
                    preNode.next=pNext;
                }
                pNode=pNext;
            }
        }
        //重新赋值链表头
        first=pHead;
    }
    public static void main(String[] args) {
        Integer[] a={1,2,3,3,4,4,5};
        Test57 t=new Test57();
        for (int i = 0; i < a.length; i++) {
            t.insertTail(a[i]);
        }
        t.deleteDuplication(t.getHeadNode());
        System.out.println(t.getHeadNode().obj);
    }
}

面试题58:二叉树的下一个结点

public class Test58 {
    private class Node<T> {
        public T value;
        public Node<T> parent;
        public Node<T> lChild;
        public Node<T> rChild;
        public Node(T value) {
            this.value = value;
        }
    }
    public Node root = null;
    private int pos = 0;
    public <T> void creatBiTree(T[] value) {
        pos = 0;
        root = creatBiTree(null,root, value);
    }
    // 以node为根节点,创建一棵树,返回此根节点
    private <T> Node creatBiTree(Node parent,Node node, T[] value) {
        T t = value[pos];
        pos++;
        if (t == null) {
            return null;
        } else {
            node = new Node<T>(t);
            node.parent=parent;
            node.lChild = creatBiTree(node,node.lChild, value);
            node.rChild = creatBiTree(node,node.rChild, value);
        }
        return node;
    }
    public Node GetNext(Node pNode){
        if (pNode==null) {
            return null;
        }
        Node pNext=null;
        if (pNode.rChild!=null) {
            Node pRight=pNode.rChild;
            while (pRight.lChild!=null) {
                pRight=pRight.lChild;
            }
            pNext=pRight;
        }else {
            Node pCurrent=pNode;
            Node pParent=pNode.parent;
            while (pParent!=null&&pCurrent==pParent.rChild) {
                pCurrent=pParent;
                pParent=pParent.parent;
            }
            pNext=pParent;
        }
        return pNext;
    }
    public static void main(String[] args) {
        Object[] a = { 'a', 'b', 'd', null, null, 'e','h', null, null, 'i',null, null,'c', 'f',null, null ,'g',null, null};
        Test58 t = new Test58();
        t.creatBiTree(a);
        Node node=t.root.lChild.rChild.rChild;
        Node pNode=t.GetNext(node);
        System.out.println(pNode.value);
    }
}

面试题59:对称的二叉树

public class Test59 {
    private class Node<T extends Comparable<? super T>> {
        public T value;
        public Node<T> lChild;
        public Node<T> rChild;
        public Node(T value) {
            this.value = value;
        }
    }
    public Node root = null;
    private int pos = 0;

    public <T extends Comparable<? super T>> void creatBiTree(T[] value) {
        pos = 0;
        root = creatBiTree(root, value);
    }
    private <T extends Comparable<? super T>> Node creatBiTree(Node node, T[] value) {
        T t = value[pos];
        pos++;
        if (t == null) {
            return null;
        } else {
            node = new Node(t);
            node.lChild = creatBiTree(node.lChild, value);
            node.rChild = creatBiTree(node.rChild, value);
        }
        return node;
    }
    private boolean isSymmetrical(Node pRoot){
        return isSymmetrical(pRoot,pRoot);
    }
    public boolean isSymmetrical(Node pRoot1, Node pRoot2) {
        if (pRoot1==null&&pRoot2==null) {
            return true;
        }
        if (pRoot1==null||pRoot2==null) {
            return false;
        }
        if (pRoot1.value.compareTo(pRoot2.value)!=0) {
            return false;
        }
        return isSymmetrical(pRoot1.lChild, pRoot2.rChild)
            && isSymmetrical(pRoot1.rChild, pRoot2.lChild);
    }
    public static void main(String[] args) {
        Integer[] a = { 8, 6, 5, null, null, 7, null, null, 6, 7, null, null,5, null, null };
        Test59 t = new Test59();
        t.creatBiTree(a);
        boolean is=t.isSymmetrical(t.root);
        System.out.println(is);
    }
}

面试题60:把二叉树打印成多行

import java.util.LinkedList;

public class Test60<T> {
    private class Node<T> {
        public T value;
        public Node<T> lChild;
        public Node<T> rChild;

        public Node(T value) {
            this.value = value;
        }
    }

    public Node<T> root = null;
    private int pos = 0;

    public void creatBiTree(T[] value) {
        pos = 0;
        root = creatBiTree(root, value);
    }

    // 以node为根节点,创建一棵树,返回此根节点
    private Node<T> creatBiTree(Node node, T[] value) {
        T t = value[pos];
        pos++;
        if (t == null) {
            return null;
        } else {
            node = new Node<T>(t);
            node.lChild = creatBiTree(node.lChild, value);
            node.rChild = creatBiTree(node.rChild, value);
        }
        return node;
    }

    public void Print(Node root) {
        LinkedList<Node> queue = new LinkedList<Node>();
        queue.add(root);
        int nextLevelNodes=0;
        int toBePrinted=1;
        while (!queue.isEmpty()) {
            Node node = queue.poll();
            System.out.print(node.value+" ");
            if (node.lChild != null) {
                queue.add(node.lChild);
                nextLevelNodes++;
            }
            if (node.rChild != null) {
                queue.add(node.rChild);
                nextLevelNodes++;
            }
            toBePrinted--;
            if (toBePrinted==0) {
                System.out.println();
                toBePrinted=nextLevelNodes;
                nextLevelNodes=0;
            }
        }
    }

    public static void main(String[] args) {
        Object[] a = { 8, 6, 5, null, null, 7, null, null, 10, 9, null, null,11, null, null };
        Test60 t = new Test60();
        t.creatBiTree(a);
        t.Print(t.root);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值