二叉树前中后序的非递归实现

前中后序的非递归实现


class TreeNode{
    char value;
    TreeNode left;
    TreeNode right;

    public TreeNode(char value){
        this.value=value;
    }
}

public class BinaryTree {
    public TreeNode buildTree() {
        TreeNode root = new TreeNode('A');
        TreeNode B = new TreeNode('B');
        TreeNode C = new TreeNode('C');
        TreeNode D = new TreeNode('D');
        TreeNode E = new TreeNode('E');
        TreeNode F = new TreeNode('F');
        TreeNode G = new TreeNode('G');
        TreeNode H = new TreeNode('H');

        root.left = B;
        root.right = C;
        B.left = D;
        B.right = E;
        E.right = H;
        C.left = F;
        C.right = G;
        return root;
    }

    //1. 二叉树的前序遍历,非递归迭代实现
    //链表形式
    List<Character> preOrderTraversalNOR(TreeNode root) {
        List<Character> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                System.out.print(cur.value + " ");
                list.add(cur.value);
                cur = cur.left;
            }
            cur = stack.pop();
            cur = cur.right;
        }
        return list;
    }

    //2. 二叉树的中序遍历,非递归迭代实现
    List<Character> inOrderTraversalNOR(TreeNode root) {
        List<Character> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        while (cur != null || !stack.empty()) {
            while (cur != null) {
                stack.push(cur);
                cur = cur.left;
            }
            cur = stack.pop();
            list.add(cur.value);
            System.out.print(cur.value + " ");
            cur = cur.right;
        }
        return list;
    }


    //3. 二叉树的后序遍历,非递归迭代实现
    public List<Character> postorderTraversal(TreeNode root){
        List<Character> list = new ArrayList<>();
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode prev = null;
        while (cur != null || !stack.empty()){
            while (cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur=stack.peek();
            if(cur.right==null || cur.right==prev){
                stack.pop();
                System.out.print(cur.value+" ");
                list.add(cur.value);
                prev=cur;//防止结点被重复打印
                cur=null;
            }else {
                cur = cur.right;
            }
        }
        return list;
    }



    public static void main(String[] args) {
        //1. 二叉树的前序遍历,非递归迭代实现
        BinaryTree binaryTree=new BinaryTree();
        TreeNode root=binaryTree.buildTree();
        binaryTree.preOrderTraversalNOR(root);
        System.out.println();
        //2. 二叉树的中序遍历,非递归迭代实现
        binaryTree.inOrderTraversalNOR(root);
        //3. 二叉树的后序遍历,非递归迭代实现
        System.out.println();
        binaryTree.postorderTraversal(root);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值