Java 二叉树的四种递归与非递归遍历

二叉树基础知识,四种遍历分别为前序、中序、后序,层次遍历。

前序的递归/非递归遍历代码为:

    public static void main(String[] arg) {
        Node1 a = new Node1('A');
        a.left=new Node1('B');
        a.rigth=new Node1('C');
        a.left.left=new Node1('D');
        a.left.rigth=new Node1('E');
        a.rigth.left=new Node1('F');
        a.rigth.rigth=new Node1('G');
        System.out.println("前序递归输出:");
        perOrderRecur(a);//递归输出
        System.out.println("\n"+"前序非递归输出:");
        perOrderRecurs(a);//递归输出

    }

    private static void perOrderRecurs(Node1 a) {
        if(a==null) {
            return ;
        }
        ArrayList<Node1> list=new ArrayList<>();
        Stack<Node1> stack=new Stack<Node1>();
        stack.push(a);
        while(!stack.isEmpty()) {
            Node1 nd=stack.pop();
            list.add(nd);
            if(nd.rigth!=null) {
                stack.push(nd.rigth);
            }
            if(nd.left!=null) {
                stack.push(nd.left);
            }
        }
        for(int i=0;i<list.size();i++) {
            System.out.print(list.get(i).val+" ");
        }
    }

    private static void perOrderRecur(Node1 a) {
        if(a==null) {
            return ;
        }
        System.out.print(a.val+" ");
        perOrderRecur(a.left);
        perOrderRecur(a.rigth);
    }

这里写图片描述

中序的递归/非递归遍历代码为:

    public static void main(String[] arg) {
        Node1 a = new Node1('A');
        a.left=new Node1('B');
        a.rigth=new Node1('C');
        a.left.left=new Node1('D');
        a.left.rigth=new Node1('E');
        a.rigth.left=new Node1('F');
        a.rigth.rigth=new Node1('G');
        System.out.println("中序递归输出:");
        inOrderRecur(a);//递归输出
        System.out.println("\n"+"中序非递归输出:");
        inOrderRecurs(a);//递归输出

    }
private static void inOrderRecurs(Node1 a) {
    if(a==null) {
        return ;
    }
    ArrayList<Node1> list=new ArrayList<Node1>();
    Stack<Node1> stack=new Stack<Node1>();
    while(!stack.isEmpty()||a!=null) {
        if(a!=null) {
            stack.push(a);
            a=a.left;
        }else {
            Node1 nd=stack.pop();
            list.add(nd);
            a=nd.rigth;
        }

    }
    for(int i=0;i<list.size();i++) {
        System.out.print(list.get(i).val+" ");
    }
}


private static void inOrderRecur(Node1 a) {
    if(a==null) {
        return ;
    }
    inOrderRecur(a.left);
    System.out.print(a.val+" ");
    inOrderRecur(a.rigth);
}

这里写图片描述

后序的递归/非递归遍历代码为:

public static void main(String[] arg) {
    Node1 a = new Node1('A');
    a.left=new Node1('B');
    a.rigth=new Node1('C');
    a.left.left=new Node1('D');
    a.left.rigth=new Node1('E');
    a.rigth.left=new Node1('F');
    a.rigth.rigth=new Node1('G');
    System.out.println("后序递归输出:");
    posOrderRecur(a);//递归输出
    System.out.println("\n"+"后序非递归输出:");
    posOrderRecurs(a);//递归输出

}


private static void posOrderRecurs(Node1 a) {
    if(a==null) {
        return ;
    }
    Stack<Node1> stack1=new Stack<Node1>();
    stack1.push(a);
    Stack<Node1> stack2=new Stack<Node1>();
    while(!stack1.isEmpty()) {
        Node1 nd=stack1.pop();
        stack2.push(nd);
        if(nd.left!=null) {
            stack1.push(nd.left);
        }
        if(nd.rigth!=null) {
            stack1.push(nd.rigth);
        }
    }
    while(!stack2.isEmpty()) {
        System.out.print(stack2.pop().val+" ");
    }
}


private static void posOrderRecur(Node1 a) {
    if(a==null) {
        return ;
    }
    posOrderRecur(a.left);
    posOrderRecur(a.rigth);
    System.out.print(a.val+" ");
}

这里写图片描述

层次的非递归遍历代码为:

public static void main(String[] arg) {
    Node a = new Node('A');
    a.left=new Node('B');
    a.rigth=new Node('C');
    a.left.left=new Node('D');
    a.left.rigth=new Node('E');
    a.rigth.left=new Node('F');
    a.rigth.rigth=new Node('G');
    System.out.println("层次遍历非递归为:");
    levelRecur(a);
}

private static void levelRecur(Node a) {
    if(a==null) {
        return;
    }
    Queue<Node> queue=new LinkedList<Node>();
    queue.offer(a);
    while(!queue.isEmpty()) {
        Node nd=queue.poll();
        System.out.print(nd.val+" ");
        if(nd.left!=null) {
            queue.offer(nd.left);
        }
        if(nd.rigth!=null) {
            queue.offer(nd.rigth);
        }
    }

}

这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值