程序员代码面试指南第二版 40.二叉树的按层打印与ZigZag打印

welcome to my blog

程序员代码面试指南第二版 40.二叉树的按层打印与ZigZag打印

题目描述
给定一颗二叉树,分别实现按层和 ZigZag 打印二叉树。
ZigZag遍历: 意思是第一层从左到右遍历,第二层从右到左遍历,依次类推。

输入描述:
第一行输入两个整数 n 和 root,n 表示二叉树的总节点个数,root 表示二叉树的根节点。
以下 n 行每行三个整数 fa,lch,rch,表示 fa 的左儿子为 lch,右儿子为 rch。(如果 lch 为 0 则表示 fa 没有左儿子,rch同理)

输出描述:
先输出按层打印,再输出按ZigZag打印。

示例1

输入

8 1
1 2 3
2 4 0
4 0 0
3 5 6
5 7 8
6 0 0
7 0 0
8 0 0

输出
Level 1 : 1
Level 2 : 2 3
Level 3 : 4 5 6
Level 4 : 7 8
Level 1 from left to right: 1
Level 2 from right to left: 3 2
Level 3 from left to right: 4 5 6
Level 4 from right to left: 8 7
第一次做; 核心:记录层数, 可以使用countdown, num, level三个变量共同控制, 其中,countdown表示当前层还有多少节点没处理, 从第一层开始记录, 第一层还剩1个根节点没有处理; num记录下一层节点的数量, 从第二层开始记录, 第二层就是根节点的下一层; level表示层数, 根节点在第一层
import java.util.Scanner;
import java.util.LinkedList;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        String[] str = sc.nextLine().split(" ");
        int n = Integer.parseInt(str[0]);
        int rootVal = Integer.parseInt(str[1]);
        TreeNode[] nodes = new TreeNode[n+1];
        for(int i=1; i<n+1; i++){
            nodes[i] = new TreeNode(i);
        }
        for(int i=0; i<n; i++){
            str = sc.nextLine().split(" ");
            int fa = Integer.parseInt(str[0]);
            int left = Integer.parseInt(str[1]);
            int right = Integer.parseInt(str[2]);
            nodes[fa].left = nodes[left];
            nodes[fa].right = nodes[right];
        }
        //
        //关键在于记录层数
        levelTraverse(nodes[rootVal]);
        zigzagTraverse(nodes[rootVal]);



    }
    public static void zigzagTraverse(TreeNode root){
        if(root==null)
            return;
        LinkedList<TreeNode> deque = new LinkedList<>();
        deque.add(root);
        //下一层节点的数量, 从第二层开始记录, 第二层就是根节点的下一层
        int num = 0;
        //当前层还有多少节点没处理, 从第一层开始记录, 第一层还剩1个根节点没有处理
        int countdown = 1;
        //记录层数
        int level = 1;
        // 表示奇数层和偶数层, 约定: true表示奇数层, false表示偶数层; 从奇数层开始
        boolean flag = true;
        while(!deque.isEmpty()){
            //奇数层, 从队列头弹出元素, 打印; 左孩子先进队尾, 右孩子后进队尾
            if(flag==true){
                System.out.print("Level "+level+" from left to right: ");
                while(countdown>0){
                    TreeNode cur = deque.poll();
                    System.out.print(cur.val+" ");
                    countdown--;
                    if(cur.left!=null){
                        deque.add(cur.left);
                        num++;
                    }
                    if(cur.right!=null){
                        deque.add(cur.right);
                        num++;
                    }
                }
                System.out.println();
                //update; 下面这四句可以写到if else 的外面
                countdown = num;
                num = 0;
                flag = false;
                level++;
            }
            //偶数层, 从队尾弹出元素, 打印; 右孩子先进队首, 左孩子后进队首
            else{
                System.out.print("Level "+level+" from right to left: ");
                while(countdown>0){
                    TreeNode cur = deque.pollLast();
                    System.out.print(cur.val+" ");
                    countdown--;
                    if(cur.right!=null){
                        deque.addFirst(cur.right);
                        num++;
                    }
                    if(cur.left!=null){
                        deque.addFirst(cur.left);
                        num++;
                    }
                }
                System.out.println();
                //update
                countdown = num;
                num = 0;
                flag = true;
                level++;
            }
        }
    }
    public static void levelTraverse(TreeNode root){
        if(root==null)
            return;
        LinkedList<TreeNode> queue = new LinkedList<>();
        int level = 1;
        //num是下一层节点的数量; countdown是当前层还有几个节点没处理
        int num = 0, countdown = 1;
        queue.add(root);
        while(!queue.isEmpty()){
            System.out.print("Level "+level+" : ");
            while(countdown>0){
                TreeNode cur = queue.poll();
                System.out.print(cur.val+" ");
                countdown--;
                if(cur.left!=null){
                    queue.add(cur.left);
                    num++;
                }
                if(cur.right!=null){
                    queue.add(cur.right);
                    num++;
                }
            }
            System.out.println();
            //update
            countdown = num;
            num = 0;
            level++;
        }
    }
    public static class TreeNode{
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode(int val){
            this.val = val;
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值