层序遍历二叉树类型

1.从上往下打印二叉树(层序遍历)


题目: 往下打印出二叉树的每个节点,同层节点从左至右打印。


思路:使用队列的先进先出的特性,在把队列中的节点取出同时把它的儿子插入队列,从而实现按层顺序打印。

 //层序遍历
    public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        ArrayList<Integer> list = new ArrayList<>();
        TreeNode cur = null;
        
        queue.add(root);
        if(root == null){
            return list;
        }
        while(!queue.isEmpty()){
            cur = queue.poll();
            list.add(cur.val);
            System.out.println(cur.val);
            if(cur.left != null){
                queue.add(cur.left);
            }
            if(cur.right != null){
                queue.add(cur.right);
            }
        }
        return list;
    }


2.把二叉树打印成多行


思路 : 在层序遍历的基础上,需要记录下每一层的个数,在队列中弹出该层所有节点时,需要换一层输出。

代码如下:

  //使用curNum 记录当前层的节点个数,使用nextNum记录下一层的节点个数,每次对当前层节点做出队操作时,
    // 都需要对curNum-- ,并在该弹出节点的子节点做入队操作时对nextNum做加操作。 ArrayList<ArrayList<Integer>> Print(TreeNode pRoot){

        int curNum = 0 ;
        int nextNum = 0;
        
        Queue<TreeNode> queue = new LinkedList<>();
        ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
        queue.add(pRoot);
        curNum = 1;
        if(pRoot == null){
           
           return lists;
        }
        while (!queue.isEmpty()){
            ArrayList<Integer> list = new ArrayList<>();
            while (curNum > 0){
                TreeNode cur = queue.poll();
                list.add(cur.val);
                if(cur.left != null){
                    queue.add(cur.left);
                    nextNum ++;
                }
                if(cur.right != null){
                    queue.add(cur.right);
                    nextNum ++;
                }
                curNum -- ;
            }
            lists.add(list);
            curNum = nextNum;
            nextNum = 0;
        }
        return lists;
    }




3.按之字形顺序打印二叉树


题目: 请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。

第一个方法是,在按层打印的基础上,在奇数层时按序打印,在偶数层时进行reverser反转操作。该方法在海量数据时,效率低。

    //第一个方法是,在按层打印的基础上,在奇数层时按序打印,在偶数层时进行reverser反转操作。该方法在海量数据时,效率低。

    public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
            int curNum = 0 ;
            int nextNum = 0 ;
            int level = 0;
            ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
            Queue<TreeNode> queue = new LinkedList<>();

            queue.add(pRoot);
            curNum = 1;

            while (!queue.isEmpty()){
                ArrayList<Integer> list = new ArrayList<>();
                while (curNum > 0 ){
                    TreeNode cur = queue.poll();
                    list.add(cur.val);
                    if(cur.left != null){
                        queue.add(cur.left);
                        nextNum++;
                    }
                    if(cur.right != null){
                        queue.add(cur.right);
                        nextNum++;
                    }
                    curNum --;
                }
                if(level % 2 == 0){
                    lists.add(list);
                }
                else {
                    lists.add(reveser(list));
                }
                curNum = nextNum ;
                nextNum = 0;
                level ++;
            }
    }

    private ArrayList<Integer> reveser(ArrayList<Integer> list){
        int l = 0;
        int r = list.size()-1;
        while(l < r){
            int temp = list.get(l);
            list.set(l, list.get(r)) ;
            list.set(r,temp );
        }
        return list;
    }


第二个方法是,使用栈的先进后出的特性,使用两个栈,倒换从而实现。
//第二个方法是,使用栈的先进后出的特性,使用两个栈,倒换从而实现。
    public ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {

        //偶数层时,使用stack1
        Stack<TreeNode> stack1 = new Stack<>();
        //奇数层时,使用stack2
        Stack<TreeNode> stack2 = new Stack<>();

        ArrayList<ArrayList<Integer>> lists = new ArrayList<>();
        int level = 1 ;
        stack1.push(pRoot);
        while (!stack1.isEmpty() || !stack2.isEmpty()){
            if(level % 2 != 0){
                ArrayList<Integer> list = new ArrayList<>();
                while (!stack1.isEmpty()){
                    TreeNode cur = stack1.pop();
                    if(cur != null){
                        list.add(cur.val);
                        stack2.push(cur.left);
                        stack2.push(cur.right);
                    }
                }
                if(list.size() != 0){
                    lists.add(list);
                    level ++;
                }
            }
            else {
                ArrayList<Integer> list = new ArrayList<>();
                while(!stack2.isEmpty()){
                    TreeNode cur = stack2.pop();
                    if(cur != null){
                        list.add(cur.val);
                        stack1.push(cur.right);
                        stack1.push(cur.left);
                    }

                }
                if(list.size() != 0){
                    lists.add(list);
                    level ++;
                }
            }
        }
        return lists;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值