二叉树的前中后序非递归遍历

嗯…只可意会不可言传,上代码。

import java.util.Stack;

public class BTree {
    BTree lchild;
    BTree rchild;
    int val;

    BTree(int val) {
        this.val = val;
        lchild = rchild = null;
    }

    public static void main(String[] args) {
        BTree root = new BTree(1);
        root.lchild = new BTree(2);
        root.rchild = new BTree(3);
        root.lchild.lchild = new BTree(4);
        root.lchild.rchild = new BTree(5);
        root.lchild.rchild.lchild = new BTree(6);
        root.lchild.rchild.lchild.lchild = new BTree(7);
        root.lchild.rchild.lchild.rchild = new BTree(8);
        root.lchild.rchild.lchild.rchild.lchild = new BTree(9);
        root.lchild.rchild.lchild.rchild.rchild = new BTree(10);
        post_print(root);
        System.out.println();
        _post_print(root);
    }

    public static void visit(BTree node) {
        System.out.print(node.val + " ");
    }

    // 前序遍历
    public static void pre_print(BTree root) {
        if (root != null) {
            visit(root);
            pre_print(root.lchild);
            pre_print(root.rchild);
        }
    }

    public static void _pre_print(BTree root) {
        Stack<BTree> stack = new Stack<BTree>();
        if (root != null) {
            stack.push(root);
            while (!stack.isEmpty()) {
                root = stack.pop();
                visit(root);
                if (root.rchild != null) {
                    stack.push(root.rchild);
                }
                if (root.lchild != null) {
                    stack.push(root.lchild);
                }
            }
        }

    }

    // 中序遍历
    public static void mid_print(BTree root) {
        if (root != null) {
            mid_print(root.lchild);
            visit(root);
            mid_print(root.rchild);
        }
    }

    public static void _mid_print(BTree root) {
        Stack<BTree> stack = new Stack<BTree>();
        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.push(root);
                root = root.lchild;
            }
            root = stack.pop();
            visit(root);
            root = root.rchild;
        }
    }

    // 后序遍历
    public static void post_print(BTree root) {
        if (root != null) {
            post_print(root.lchild);
            post_print(root.rchild);
            visit(root);
        }
    }

    public static void _post_print(BTree root) {
        Stack<BTree> stack = new Stack<BTree>();
        boolean visited = false;
        while (!stack.isEmpty() || root != null) {
            while (root != null && !visited) {
                stack.push(root);
                root = root.lchild;
            }
            BTree top = stack.peek();
            if(top.rchild != null && !visited){
                root = top.rchild;
                visited = false;
            } else {
                BTree q= stack.pop();
                visit(q);
                //如果当前根节点被访问了,而它的右节点不为空,则说明该根节点的右子树已经访问完毕。
                if(q.rchild != null){
                    visited = false;
                }
                if(stack.isEmpty()){
                    break;
                }
                //假如此时遍历的是根节点的右子树根节点
                root = stack.peek().rchild;
                if(root == q){
                    visited = true;
                }
            }
        }
    }
}

后序非递归遍历可能写的有点复杂,如果有更简单的代码欢迎告知。
巧妙后序遍历

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值