二叉树的遍历

二叉树的各种遍历

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Stack;

/**
 * 二叉树的遍历:
 * 递归和非递归先序、中序、后序遍历
 * 层次遍历:不分层层次遍历、分层层次遍历、之字形遍历
 */

class TreeNode {
    public int val;
    public TreeNode left;
    public TreeNode right;
    public TreeNode( int val){
        this.val = val;
    }
}

public class TreeNodeTraversal {

    //先序遍历的递归实现
    public static void preOrderRecur(TreeNode root){
        if (root == null){
            return;
        }
        System.out.print(root.val + " ");
        preOrderRecur(root.left);
        preOrderRecur(root.right);
    }

    //先序遍历的非递归实现
    public static void preOrderUnRecur(TreeNode root){
        if (root != null){
            Stack<TreeNode> stack = new Stack<>();
            stack.push(root);
            while (!stack.empty()){
                root = stack.pop();
                System.out.print(root.val + " ");
                if (root.right != null){
                    stack.push(root.right);
                }
                if (root.left != null){
                    stack.push(root.left);
                }
            }
        }
    }

    //中序遍历的递归实现
    public static void inOrderRecur(TreeNode root){
        if (root == null){
            return;
        }
        inOrderRecur(root.left);
        System.out.print(root.val + " ");
        inOrderRecur(root.right);
    }

    //中序遍历的非递归实现
    public static void inOrderUnRecur(TreeNode root){
        if(root != null){
            Stack<TreeNode> stack = new Stack<>();
            while (!stack.empty() || root != null){
                if (root != null){
                    stack.push(root);
                    root = root.left;
                }else {
                    root = stack.pop();
                    System.out.print(root.val + " ");
                    root = root.right;
                }
            }
        }
    }

    //后序遍历的递归实现
    public static void postOrderRecur(TreeNode root){
        if (root == null){
            return;
        }
        inOrderRecur(root.left);
        inOrderRecur(root.right);
        System.out.print(root.val + " ");
    }

    //后序遍历的非递归实现
    public static void postOrderUnRecur(TreeNode root){
        if (root != null){
            Stack<TreeNode> stack1 = new Stack<>();
            Stack<TreeNode> stack2 = new Stack<>();
            stack1.push(root);
            while (!stack1.empty()){
                root = stack1.pop();
                stack2.push(root);
                if (root.left != null){
                    stack1.push(root.left);
                }
                if (root.right != null){
                    stack1.push(root.right);
                }
            }
            while (!stack2.empty()){
                System.out.print(stack2.pop().val + " ");
            }
        }
    }

    //不分层的层次遍历
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();//保存结果
        if (root == null){
            return result;
        }

        //用ArrayList来模拟队列
        ArrayList<TreeNode> queue = new ArrayList<>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TreeNode temp = queue.remove(0);
            result.add(temp.val);
            if (temp.left != null) {
                queue.add(temp.left);
            }
            if (temp.right != null) {
                queue.add(temp.right);
            }
        }
        return result;
    }

    //分层层次遍历
    public ArrayList<ArrayList<Integer> > PrintFC(TreeNode root) {
        ArrayList<ArrayList<Integer> > result = new ArrayList<>();//保存结果
        if (root == null){
            return result;
        }
        int count = 0;//保存当前行的节点个数
        ArrayList<Integer> list = null;
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()){
            list = new ArrayList<>();
            count = queue.size();//当前行的节点数
            while (count-- > 0){
                TreeNode temp = queue.poll();
                list.add(temp.val);
                if (temp.left != null){
                    queue.offer(temp.left);
                }
                if (temp.right != null){
                    queue.offer(temp.right);
                }
            }
            result.add(list);
        }
        return result;
    }

    //分层之字形遍历
    public static ArrayList<ArrayList<Integer> > PrintZ(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> result = new ArrayList<>();//保存最终结果
        if(pRoot==null){
            return result;
        }
        Stack<TreeNode>[] stacks = new Stack[2];//保存中间节点
        stacks[0] = new Stack<>();
        stacks[1] = new Stack<>();
        int flag = 0;
        stacks[0].push(pRoot);
        ArrayList<Integer> list = new ArrayList<>();
        while(!stacks[flag].empty()){//待输出元素不为空
            TreeNode temp = stacks[flag].pop();
            list.add(temp.val);
            //根据当前的打印方向,判断下一行的入栈顺序
            if(flag==0){
                if(temp.left!=null){
                    stacks[1-flag].push(temp.left);
                }
                if(temp.right!=null){
                    stacks[1-flag].push(temp.right);
                }
            }else{
                if(temp.right!=null){
                    stacks[1-flag].push(temp.right);
                }
                if(temp.left!=null){
                    stacks[1-flag].push(temp.left);
                }
            }
            //当前行的元素打印完毕
            if(stacks[flag].empty()){
                result.add(list);
                list = new ArrayList<>();
                flag = 1-flag;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        TreeNode node1 = new TreeNode(8);
        TreeNode node2 = new TreeNode(10);
        TreeNode node3 = new TreeNode(6);
        TreeNode node4 = new TreeNode(5);
        TreeNode node5 = new TreeNode(7);
        TreeNode node6 = new TreeNode(9);
        TreeNode node7 = new TreeNode(11);
        node1.left = node2;
        node1.right = node3;
        node2.left = node4;
        node2.right = node5;
        node3.left = node6;
        node3.right = node7;

        System.out.println(PrintZ(node1));
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值