java算法面试题之一

题目:int values[] = {80099, 16114, 63108, 25032, 31044, 59069, 39099, 13110, 34101, 66120,19116, 72105, 70045, 38032, 41110, 12105, 75110, 27105, 1105, 9114,67117, 20101, 21100, 11032, 79046, 32112, 5111, 6117, 45116, 22032,61097, 65120, 49110, 15101, 82109, 50103, 54110, 17101, 46032, 4121,
76097, 7032, 57105, 2102, 58044, 8097, 44099, 73064, 81111, 43097,30112, 14116, 60109, 74104, 77105, 35097, 64058, 29112, 55032, 33108,71108, 40111, 47088, 52117, 56076, 68097, 37101, 78114, 24110, 53097,69110, 48105, 18115, 26072, 3032, 42116, 62105, 51120, 28065, 10101,23105, 36115}
这样一个数组,操作如下。
1.首先将数组的内容有序的存入二叉树中。
2.然后由小到大遍历二叉树。
3.把遍历到的数值取后三位转化成字符串输出。


思路
- 首先要将数组的内容有序的存入二叉树中,那么我们可以先将数组排序,排序之后再插入到二叉树中,然后采用层级遍历二叉树的方法,输出数值,在输出数值的时候用String的方法截取后三位。
代码如下:

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

    public TreeNode(int value) {
        this(value, null, null);
    }

    public TreeNode(int value, TreeNode left, TreeNode right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }

}
// 对二叉树的操作
public class Tree {

    public TreeNode root;

    public Tree(TreeNode root) {
        this.root = root;
    }

    /***
     * 前序遍历
     * @param node
     */
    public void preLeft(TreeNode node) {
        if (node != null) {
            System.out.print(node.value + "\t");
            preLeft(node.left);
            preLeft(node.right);
        }
    }

    /***
     * 中序遍历
     * @param node
     */
    public void preMiddle(TreeNode node) {
        if (node != null) {
            preLeft(node.left);
            System.out.print(node.value + "\t");
            preLeft(node.right);
        }
    }

    /**
     * 后序遍历
     * @param node
     */
    public void preRight(TreeNode node) {
        if (node != null) {
            preLeft(node.left);
            preLeft(node.right);
            System.out.print(node.value + "\t");
        }
    }

    /***
     * 层序遍历
     * @param root
     */
    public static void preLevel(TreeNode root) {
        if (root == null) return;
        Queue<TreeNode> q = new LinkedList<TreeNode>();
        q.add(root);
        while (!q.isEmpty()) {
            TreeNode temp = q.poll();
            System.out.print(temp.value + "\t");
            if (temp.left != null) q.add(temp.left);
            if (temp.right != null) q.add(temp.right);
        }
    }

    /***
     * 层序遍历
     * @param treeNode
     */
    public void preLevel2(TreeNode treeNode) {
        if (treeNode != null) {
            Queue<TreeNode> queue = new LinkedList<>();
            queue.add(treeNode);
            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();

                System.out.print(convert(node.value) + "\t");
                if (node.left != null) {
                    queue.add(node.left);
                }
                if (node.right != null) {
                    queue.add(node.right);
                }
            }
        }
    }

    /**
     * 拿到数值的后三位
     * @param value
     * @return
     */
    public String convert(int value){
        String valueStr = String.valueOf(value);
        int length = valueStr.length();
        if(length > 3){
            return valueStr.substring(length-3);
        }
        return null;
    }

    /***
     * 获取所有结点
     * @param node
     * @return
     */
    public int getCountNode(TreeNode node) {
        int count = 0;
        if (node != null) {
            count += 1;
            count += getCountNode(node.left);
            count += getCountNode(node.right);
        }
        return count;
    }

    /**
     * 树的深度
     *
     * @param treeNode
     * @return
     */
    public int getDepth(TreeNode treeNode) {
        if (treeNode != null) {
            int leftDepth = getDepth(treeNode.left);
            int rightDepth = getDepth(treeNode.right);
            return 1 + ((leftDepth > rightDepth) ? leftDepth : rightDepth);
        }
        return 0;
    }

}
// 测试
public class Test {

    public static void main(String[] args) {
        int values[] = {80099, 16114, 63108, 25032, 31044, 59069, 39099, 13110, 34101, 66120, 19116, 72105, 70045, 38032, 41110, 12105, 75110, 27105, 1105, 9114, 67117, 20101, 21100, 11032, 79046, 32112, 5111, 6117, 45116, 22032, 61097, 65120, 49110, 15101, 82109, 50103, 54110, 17101, 46032, 4121,
                76097, 7032, 57105, 2102, 58044, 8097, 44099, 73064, 81111, 43097, 30112, 14116, 60109, 74104, 77105, 35097, 64058, 29112, 55032, 33108, 71108, 40111, 47088, 52117, 56076, 68097, 37101, 78114, 24110, 53097, 69110, 48105, 18115, 26072, 3032, 42116, 62105, 51120, 28065, 10101, 23105, 36115};
        int length = values.length;
        // 使用冒泡排序将数组有序化
        for (int i = 0; i < length; i++) {
            for (int j = 0; j < length - i - 1; j++) {
                if (values[j] > values[j + 1]) {
                    int temp = values[j];
                    values[j] = values[j + 1];
                    values[j + 1] = temp;
                }
            }
        }
        for (int i = 0; i < length; i++) {
            System.out.print(values[i] + "\t");
        }
        // 生成一颗二叉树
        TreeNode root = createBinaryTree(values, 0);
        Tree tree = new Tree(root);
        // 使用各种方法遍历
        System.out.println("\n层序遍历");
        tree.preLevel(root);
        System.out.println("\n层序遍历打印后三位");
        tree.preLevel2(root);
        System.out.println("\n左序遍历");
        tree.preLeft(root); // 左序遍历
        System.out.println("\n中序遍历");
        tree.preMiddle(root); // 中序遍历
        System.out.println("\n右序遍历");
        tree.preRight(root); // 右序遍历

        System.out.println("\n树的深度是");
        int count = tree.getDepth(root);
        System.out.println(count);

    }

    /***
     * 创建一颗二叉树
     * @param values 值
     */
    public static TreeNode createBinaryTree(int[] values, int index) {
        TreeNode root = null;
        if (index < values.length) {
            root = new TreeNode(values[index]);
            root.left = createBinaryTree(values, 2 * index + 1);
            root.right = createBinaryTree(values, 2 * index + 2);
        }
        return root;
    }

}

完事

猜你喜欢

转载自blog.csdn.net/u010648159/article/details/78875859