二叉树的先序,中序,后序遍历有递归和无递归

Node结构

public static class Node{
        private int value;
        private Node left;
        private Node right;

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public Node getLeft() {
            return left;
        }

        public void setLeft(Node left) {
            this.left = left;
        }

        public Node getRight() {
            return right;
        }

        public void setRight(Node right) {
            this.right = right;
        }
    }

先序递归方式

 public static void pre(Node head){
        if(head == null){
            return ;
        }
        System.out.println(head.value);
        pre(head.left);
        pre(head.right);
    }

中序递归方式

public static void in(Node head){
        if(head == null){
            return ;
        }
        in(head.left);
        System.out.println(head.value);
        in(head.right);
    }

后序递归方式

public static void post(Node head){
        if(head == null){
            return ;
        }
        post(head.left);
        post(head.right);
        System.out.println(head.value);
    }

先序无递归方式

public static void pre1(Node head){
        if(head == null){
            return ;
        }
        Stack<Node> stack = new Stack<>();
        stack.push(head);
        while (!stack.isEmpty()){
            head = stack.pop();
            System.out.println(head.value);
            if(head.right != null){
                stack.push(head.right);
            }
            if(head.left != null){
                stack.push(head.left);
            }
        }
    }

中序无递归方式

public static void in2(Node head){
        if(head == null){
            return ;
        }
        Stack<Node> stack = new Stack<>();
        while (!stack.isEmpty() || head != null){
            if(head != null){
                stack.push(head);
                head = head.left;
            }else{
                head = stack.pop();
                System.out.println(head.value);
                head = head.right;
            }
        }
    }

后序无递归方式

public static void post2(Node head){
        if(head == null){
            return ;
        }
        Stack<Node> stack = new Stack<>();
        Stack<Node> stack1 = new Stack<>();
        stack.push(head);
        while (!stack.isEmpty()){
            stack1.push(stack.pop());
            if(head.left != null){
                stack.push(head.left);
            }
            if(head.right != null){
                stack.push(head.right);
            }
        }
        while (!stack1.isEmpty()){
            System.out.println(stack1.pop().value);
        }
    }

后序无递归还有一个省空间的方式,自己没理解透,以后再说。。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值