《程序员代码面试指南》--二叉树中

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43928720/article/details/102689507

1. 重建二叉树

题目:
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。假设输入的前序遍历和中序遍历的结果中都不包含重复的数字。例如输入前序遍历序列{1, 2, 4, 7, 3, 5, 6, 8}和中序遍历序列{4, 7, 2, 1,5, 3, 8, 6},则重建出二叉树并输出它的头结点。
代码:

package charpter3;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2019-10-14 20:50
 * @desc 重建二叉树
 */
public class test6 {
    public static Node rebuildTree(int[] preOrder, int[] indiOrder) {

        if (preOrder == null || indiOrder == null) {
            return null;
        }
        return rebuildTreeCore(preOrder, 0, preOrder.length - 1, indiOrder, 0, indiOrder.length - 1);
    }

    public static Node rebuildTreeCore(int[] preOrder, int startPre, int endPre, int[] indiOrder, int startIndi, int endIndi) {
        if (startPre > endPre || startIndi > endIndi) {
            return null;
        }
        Node root = new Node(preOrder[startPre]);
        for (int i = startIndi; i <= endIndi; i++) {
            if (preOrder[startPre] == indiOrder[i]) {
                root.left = rebuildTreeCore(preOrder, startPre + 1, startPre + (i - startIndi), indiOrder, startIndi, i - 1);
                root.right = rebuildTreeCore(preOrder, (i - startIndi) + startPre + 1, endPre, indiOrder, i + 1, endIndi);
            }
        }
        return root;
    }

    public static void main(String[] args) {
        int pre[] = {4, 2, 1, 3, 6, 5, 7};
        int indi[] = {1, 2, 3, 4, 5, 6, 7};
        Node head = rebuildTree(pre, indi);
        System.out.println(head.data);
    }
}

2. 遍历二叉树的神级方法

package charpter3;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2019-10-16 19:35
 * @desc 二叉树遍历的神级morris算法,时间复杂度为O(N),空间复杂度为O(1)
 */
public class test7 {
    /**
     * 中序遍历
     *
     * @param head
     */
    public static void morrisIn(Node head) {
        if (head == null) {
            return;
        }
        Node cur1 = head;
        Node cur2 = null;
        while (cur1 != null) {
            cur2 = cur1.left;
            if (cur2 != null) {
                while (cur2.right != null && cur2.right != cur1) {
                    cur2 = cur2.right;
                }
                if (cur2.right == null) {
                    cur2.right = cur1;
                    cur1 = cur1.left;
                    continue;
                } else {
                    cur2.right = null;
                }
            }
            System.out.println(cur1.data);
            cur1 = cur1.right;

        }
    }

    /**
     * 前序遍历
     *
     * @param head
     */
    public static void morrisPre(Node head) {
        if (head == null) {
            return;
        }
        Node cur1 = head;
        Node cur2 = null;
        while (cur1 != null) {
            cur2 = cur1.left;
            if (cur2 != null) {
                while (cur2.right != null && cur2.right != cur1) {
                    cur2 = cur2.right;
                }
                if (cur2.right == null) {
                    System.out.println(cur1.data);
                    cur2.right = cur1;
                    cur1 = cur1.left;
                    continue;
                } else {
                    cur2.right = null;
                }
            } else {
                System.out.println(cur1.data);
            }
            cur1 = cur1.right;

        }
    }

    /**
     * 后序遍历
     *
     * @param head
     */
    public static void morrisPost(Node head) {
        if (head == null) {
            return;
        }
        Node cur1 = head;
        Node cur2 = null;
        while (cur1 != null) {
            cur2 = cur1.left;
            if (cur2 != null) {
                while (cur2.right != null && cur2.right != cur1) {
                    cur2 = cur2.right;
                }
                if (cur2.right == null) {
                    cur2.right = cur1;
                    cur1 = cur1.left;
                    continue;
                } else {
                    cur2.right = null;
                    printEdge(cur1.left);
                }
            }
            cur1 = cur1.right;
        }
        printEdge(head);
    }

    public static void printEdge(Node head) {
        Node tail = reverseEdge(head);
        Node cur = tail;
        while (cur != null) {
            System.out.println(cur.data);
            cur = cur.right;
        }
        reverseEdge(tail);
    }

    public static Node reverseEdge(Node head) {
        Node pre = null;
        Node next = null;
        while (head != null) {
            next = head.right;
            head.right = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    public static void main(String[] args) {
        Node node1 = new Node(4);
        Node node2 = new Node(2);
        Node node3 = new Node(6);
        Node node4 = new Node(1);
        Node node5 = new Node(3);
        Node node6 = new Node(5);
        Node node7 = new Node(7);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;

        morrisPost(node1);
    }
}

3. 未排序数组中累加和为给定值的最长子数组长度

package charpter3;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2019-10-17 11:27
 * @desc 未排序数组中累加和为给定值的最长子数组长度
 */
public class test8 {

    public static int getMaxLength(int[] arr, int k) {
        if (arr == null || arr.length == 0 || k <= 0) {
            return 0;
        }
        int left = 0;
        int right = 0;
        int sum = arr[0];
        int len = 0;
        while (right < arr.length) {
            if (sum == k) {
                len = Math.max(len, right - left + 1);
                sum -= arr[left++];
            } else if (sum < k) {
                right++;
                if (right == arr.length) {
                    break;
                }
                sum += arr[right];
            } else {
                sum -= arr[left++];
            }
        }
        return len;

    }

    public static void main(String[] args) {
        int[] arr = {1, 7, 5, 4, 1, 8, 0, 4};
        System.out.println(getMaxLength(arr, 13));
    }
}

4. 在二叉树中找到累加和为指定值的最长路径长度

题目描述:
给定一棵二叉树的头结点head和一个32位整数sum,二叉树节点值类型为整型, 求累加和为sum的最长路径长度。路径是指从某个节点往下,每次最多选择一个孩子节点或者不选所形成的节点链。

代码:

package charpter3;

import java.util.HashMap;
import java.util.Map;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2019-10-21 09:44
 * @desc
 */
public class test9 {

    public static int getMaxLength(Node head, int sum) {
        HashMap<Integer, Integer> sumMap = new HashMap<>();
        sumMap.put(0, 0);
        return preOrder(head, sum, 0, 1, 0, sumMap);

    }

    public static int preOrder(Node head, int sum, int preSum, int level, int maxLength, HashMap<Integer, Integer> sumMap) {
        if (head == null) {
            return maxLength;
        }
        int curSum = preSum + head.data;
        if (!sumMap.containsKey(curSum)) {
            sumMap.put(curSum, level);
        }
        if (sumMap.containsKey(curSum - sum)) {
            maxLength = Math.max(level - sumMap.get(curSum - sum), maxLength);
        }
        maxLength = preOrder(head.left, sum, curSum, level + 1, maxLength, sumMap);
        maxLength = preOrder(head.right, sum, curSum, level + 1, maxLength, sumMap);
        if (level == sumMap.get(curSum)) {
            sumMap.remove(curSum);
        }
        return maxLength;
    }

    public static void main(String[] args) {
        Node node1 = new Node(4);
        Node node2 = new Node(2);
        Node node3 = new Node(6);
        Node node4 = new Node(1);
        Node node5 = new Node(3);
        Node node6 = new Node(5);
        Node node7 = new Node(7);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;
        System.out.println(getMaxLength(node1,7));
    }
}

5. 二叉查找树

package charpter3;

/**
 * @author chengzhengda
 * @version 1.0
 * @date 2019-10-22 10:31
 * @desc 二叉查找树
 */
public class test10 {

    public static boolean binarySearch(Node head, int k) {

        while (head != null) {
            if (k > head.data) {
                head = head.right;
            } else if (k < head.data) {
                head = head.left;
            } else {
                return true;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        Node node1 = new Node(4);
        Node node2 = new Node(2);
        Node node3 = new Node(6);
        Node node4 = new Node(1);
        Node node5 = new Node(3);
        Node node6 = new Node(5);
        Node node7 = new Node(7);

        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;

        System.out.println(binarySearch(node1, 6));
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_43928720/article/details/102689507