二叉树的相关面试题

1.递归方式实现二叉树先中后序遍历

public class Code01_DgModel {
    //定义节点的类型
    public class Node{
        public int value;
        public Node right;
        public Node left;

        public Node(int val){
            this.value = val;
        }
    }
    //先序遍历 头左右
    public void preOrderRecur(Node head){
        if(head==null){
            return;
        }
        System.out.println(head.value+ " ");
        preOrderRecur(head.left);
        preOrderRecur(head.right);
    }
    //中序遍历 左头右
    public void inOrderRecur(Node head){
        if(head==null){
            return;
        }

        inOrderRecur(head.left);
        System.out.println(head.value + " ");
        inOrderRecur(head.right);
    }
    //后续遍历 左右头
    public void inPosRecur(Node head){
        if(head==null){
            return;
        }

        inPosRecur(head.left);
        inPosRecur(head.right);
        System.out.println(head.value+ " ");
    }

}

在代码上展示打印递归版本的三种顺序的方式就是打印时机的不同

2.非递归方式实现二叉树的先中后序遍历

public class Code01_NoDgModel {

    public class Node {
        public int value;
        public Node right;
        public Node left;

        public Node(int val) {
            this.value = val;
        }
    }

    // 前序 中左右
    public void preOrderUnRecur(Node head) {

        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            //栈压节点
            stack.add(head);
            //这段代码的逻辑就是栈不空,弹出一个节点 弹出则压子节点的栈
            //遵循先有右压右后有左压左的原则 栈先进后出的作用下达到头左右的效果
            while (!stack.isEmpty()) {
                head = stack.pop();
                System.out.println(head.value + " ");

                if (head.right != null) {

                    stack.push(head.right);
                }

                if (head.left != null) {
                    stack.push(head.left);
                }

            }
            System.out.println();
        }
    }

    // 中序 左中右
    public static void inOrderUnRecur(Node head) {
        System.out.print("in-order: ");
        if (head != null) {
            Stack<Node> stack = new Stack<Node>();
            //栈不为空或头节点不为空时执行下列逻辑
            while (!stack.isEmpty() || head != null) {
                //头节点不为空压栈压到头节点的左子节点为空时跳出
                if (head != null) {
                    stack.push(head);
                    head = head.left;
                } else {
                    //执行 栈弹出打印值 到head指到右边节点 满足head不等于空再走上面if的逻辑
                    head = stack.pop();
                    System.out.print(head.value + " ");
                    head = head.right;
                }
            }
        }
        System.out.println();
    }

    // 后序
    public static void posOrderUnRecur1(Node head) {
        System.out.print("pos-order: ");
        //栈1压头节点 不为空 弹出压入栈2 栈1压节点的左右节点
        if (head != null) {
            //逻辑栈
            Stack<Node> s1 = new Stack<Node>();
            //输出栈
            Stack<Node> s2 = new Stack<Node>();
            s1.push(head);
            while (!s1.isEmpty()) {
                head = s1.pop();
                s2.push(head);
                if (head.left != null) {
                    s1.push(head.left);
                }
                if (head.right != null) {
                    s1.push(head.right);
                }
            }
            while (!s2.isEmpty()) {
                System.out.print(s2.pop().value + " ");
            }
        }
        System.out.println();
    }

}

3.寻找一个树任意节点的后继节点

后继节点:在中序遍历中节点的后一个节点叫该节点的后继节点

public class Code03_SuccessorNode {

    public static class Node{
        public int value;
        public Node left;
        public Node right;
        public Node parent;

        public Node(int data) {
            this.value = data;
        }
    }

    public static Node getSuccessorNode(Node node){
        if(node == null){
            return node;
        }
        //一个节点如果有右子树 它的后继节点就是该节点右子树上最左的节点 因为整体是中序遍历 左中右
        if(node.right!=null){
            return getLeftMost(node.right);
        }else{
            //节点无右子树的情况

            Node parent = node.parent;
            while(parent!=null&&parent.left!=node){
                node  = parent;
                parent = node.parent;
            }
            return parent;
        }
    }

    private static Node getLeftMost(Node node) {
        if(node == null){
            return node;
        }
        while(node.left!=null){
            node = node.left;
        }
        return node;
    }
}

4.二叉树的序列化和反序列化

public class Code04_SerializeAndReconstructTree {
    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static String serialByPre(Node head) {
        if (head == null) {
            return "#!";
        }
        //按照先序方式来序列化
        String res = head.value + "!";
        res += serialByPre(head.left);
        res += serialByPre(head.right);
        return res;
    }

    public static Node reconByPreString(String preStr) {
        //分割放入一个String数组中
        String[] values = preStr.split("!");
        //新建队列
        Queue<String> queue = new LinkedList<String>();
        for (int i = 0; i != values.length; i++) {
            queue.offer(values[i]);
        }
        return reconPreOrder(queue);
    }

    public static Node reconPreOrder(Queue<String> queue) {
        String value = queue.poll();
        if (value.equals("#")) {
            return null;
        }
        Node head = new Node(Integer.valueOf(value));
        head.left = reconPreOrder(queue);
        head.right = reconPreOrder(queue);
        return head;
    }
    //按层序列化
    public static String serialByLevel(Node head) {
        if (head == null) {
            return "#!";
        }
        String res = head.value + "!";
        Queue<Node> queue = new LinkedList<Node>();
        queue.offer(head);
        while (!queue.isEmpty()) {
            head = queue.poll();
            if (head.left != null) {
                res += head.left.value + "!";
                queue.offer(head.left);
            } else {
                res += "#!";
            }
            if (head.right != null) {
                res += head.right.value + "!";
                queue.offer(head.right);
            } else {
                res += "#!";
            }
        }
        return res;
    }

}

5.判断一棵树是不是平衡二叉树

public class Code05_IsBalancedTree {

    public static class Node {
        public int value;
        public Node left;
        public Node right;

        public Node(int data) {
            this.value = data;
        }
    }

    public static class ReturnData{
        public boolean isBalance;
        public int h; 
        public ReturnData(boolean isBalance,int h){
            this.isBalance = isBalance;
            this.h = h;
        }       
    }

    public static boolean isB(Node head){
        return process(head).isBalance;
    }

    public static ReturnData process(Node head){
        if(head == null){
            return new ReturnData(true,0);
        }
         ReturnData leftData = process(head.left);


        if(!leftData.isBalance){
            return new ReturnData(false,0);
        }

        ReturnData rightData = process(head.right);

        if(!rightData.isBalance){
            return new ReturnData(false,0);
        }

        if(Math.abs(leftData.h - rightData.h)>1){
            return new ReturnData(false,0);
        }

            return new ReturnData(true,Math.max(leftData.h,rightData.h)+1);
        }

        }

        public static void main(String[] args) {
        Node head = new Node(1);
        head.left = new Node(2);
        head.right = new Node(3);
        head.left.left = new Node(4);
        head.left.right = new Node(5);
        head.right.left = new Node(6);
        head.right.right = new Node(7);

        System.out.println(isBalance(head));

    }

6.判断是否是搜索二叉树/完全二叉树

判断一棵树是否是搜索二叉树
(若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;它的左、右子树也分别为二叉排序树)

public class Code06_IsBSTAndCBT {

class Node {
    int data;
    Node left;
    Node right;
}
    private static int pre = Integer.MIN_VALUE;
    public boolean isBST(Node head) {
        if(head == null){
            return true;
        }
        //先判断左子数
        boolean judgeLeft = isBST(head.left);
        //值递增并且judgeLeft为真
        if(head.data>=pre&&judgeLeft==true){
            pre = head.data;
        }else{
            return false;
        }

        boolean judgeRight = isBST(head.right);

        return judgeRight;
    }

}

猜你喜欢

转载自blog.csdn.net/qq_34591917/article/details/81951362