二叉树算法(前、中、后序)

/**********************二叉树*************************/

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    /**
     * 前 中 后序遍历
     *
     * @param node
     */
    public void preOrder(TreeNode node) {
        if (node != null) {
            System.out.println(node.val);
            preOrder(node.left);
            preOrder(node.right);
        }
    }

    public static void preOrder2(TreeNode biTree) {//非递归实现
        Stack stack = new Stack();
        while (biTree != null || !stack.isEmpty()) {
            while (biTree != null) {
                System.out.println(biTree.val);
                stack.push(biTree);
                biTree = biTree.left;
            }
            if (!stack.isEmpty()) {
                biTree = stack.pop();
                biTree = biTree.right;
            }
        }
    }

    public static void midOrder(TreeNode node) {
        if (node != null) {
            midOrder(node.left);
            System.out.println(node.val);
            midOrder(node.right);
        }
    }

    /**
     * 中序遍历非递归实现
     *
     * @param biTree
     */
    public static void midOrder2(TreeNode biTree) {
        Stack stack = new Stack();
        while (biTree != null || !stack.isEmpty()) {
            while (biTree != null) {
                stack.push(biTree);
                biTree = biTree.left;
            }
            if (!stack.isEmpty()) {
                biTree = stack.pop();
                System.out.println(biTree.val);
                biTree = biTree.right;
            }
        }
    }

    public void afterOrder(TreeNode node) {
        if (node != null) {
            afterOrder(node.left);
            afterOrder(node.right);
            System.out.println(node.val);
        }
    }

    /**
     * 后序遍历非递归实现
     *
     * @param biTree
     */
    public static void postOrder2(TreeNode biTree) {
        int left = 1;//在辅助栈里表示左节点
        int right = 2;//在辅助栈里表示右节点
        Stack stack = new Stack();
        Stack stack2 = new Stack();//辅助栈,用来判断子节点返回父节点时处于左节点还是右节点。

        while (biTree != null || !stack.empty()) {
            while (biTree != null) {//将节点压入栈1,并在栈2将节点标记为左节点
                stack.push(biTree);
                stack2.push(left);
                biTree = biTree.left;
            }

            while (!stack.empty() && stack2.peek() == right) {//如果是从右子节点返回父节点,则任务完成,将两个栈的栈顶弹出
                stack2.pop();
                System.out.println(stack.pop().val);
            }

            if (!stack.empty() && stack2.peek() == left) {//如果是从左子节点返回父节点,则将标记改为右子节点
                stack2.pop();
                stack2.push(right);
                biTree = stack.peek().right;
            }

        }
    }

    /**
     * 右试图,层序遍历,size问题
     *
     * @param root
     * @return
     */
    public List rightSideView(TreeNode root) {
        List res = new ArrayList<>();
        if (null == root) {
            return res;
        }
        Queue queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();
            for (int i = 0; i < size; i++) {
                TreeNode cur = queue.poll();
                if (i == (size - 1)) {
                    res.add(cur.val);
                }
                if (cur.left != null) {
                    queue.add(cur.left);
                }

                if (cur.right != null) {
                    queue.add(cur.right);
                }
            }
        }

        return res;
    }

    /**
     * 以数组形式生成一棵完全二叉树
     */
    public static void createTree() {
        TreeNode[] node = new TreeNode[10];
        for (int i = 0; i < 10; i++) {
            node[i] = new TreeNode(i);
        }
        for (int i = 0; i < 10; i++) {
            if (i * 2 + 1 < 10)
                node[i].left = node[i * 2 + 1];
            if (i * 2 + 2 < 10)
                node[i].right = node[i * 2 + 2];
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(node[i].val);
        }
    }

你可能感兴趣的:(二叉树算法(前、中、后序))