剑指 Offer 32 - I和II. 从上到下打印二叉树

本文介绍如何使用层序遍历的方式打印二叉树,包括从上到下、按层打印及之字形打印等不同场景,并提供详细的Java代码实现。

剑指 Offer 32 - I. 从上到下打印二叉树

剑指 Offer 32 - I. 从上到下打印二叉树

从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回:
[3,9,20,15,7]

提示:
节点总数 <= 1000

思路

就是一个层序遍历
比较简单

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public int[] levelOrder(TreeNode root) {
        //边界值
        if (root == null) return new int[]{};

        //层序遍历树🌲

        //建立一个队列
        Queue<TreeNode> q = new LinkedList<TreeNode> ();
        
        //将根节点入队
        q.add(root);

        //新建一个具有树节点个数的空数组
        int[] resultArray = new int[getTreeCount(root)];

        //数组当前位置
        int index = 0;

        //循环判断队列不为空
        while (!q.isEmpty()) {
            //取出队头元素
            TreeNode tempNode = q.poll();
            //将队头元素加入数组
            resultArray[index++] = tempNode.val;

            if (tempNode.left != null) {
                q.add(tempNode.left);
            }

            if (tempNode.right != null) {
                q.add(tempNode.right);
            }
        }

        return resultArray;
    }

    //得到树的个数
    private static int getTreeCount(TreeNode root) {
        if (root == null) return 0;
        return (getTreeCount(root.left) + getTreeCount(root.right) + 1);
    }
}

需要注意的地方:
Queue的创建


剑指 Offer 32 - II. 从上到下打印二叉树 II

剑指 Offer 32 - II. 从上到下打印二叉树 II
从上到下按层打印二叉树,同一层的节点按从左到右的顺序打印,每一层打印到一行。

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]
提示:
    节点总数 <= 1000

思路

该题也是需要层序遍历(必会)
然后,要确定二叉树的一层正好是二维数组的行(一维数组)
在之前做的题目当中,有一个求二叉树高度的题,也是同样的原理
数据结构与算法—树---翻转二叉树、二叉树的高度

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        //边界条件
        if (root == null) return new ArrayList<List<Integer>> ();

        //建立一个二维数组
        List<List<Integer>> list = new ArrayList<List<Integer>> ();

        //建立一个队列
        Queue<TreeNode> q = new LinkedList<TreeNode>();

        q.add(root);

        //队列中元素个数
        int queueCount = 1;

        List<Integer> tempList = new ArrayList<Integer> ();

        while (!q.isEmpty()) {
            TreeNode tempNode = q.poll();
            tempList.add(tempNode.val);

            queueCount--;

            if (tempNode.left != null) {
                q.add(tempNode.left);
            }

            if (tempNode.right != null) {
                q.add(tempNode.right);
            }
            

            if (queueCount == 0) {
                queueCount = q.size();

                List<Integer> subList = new ArrayList<Integer> ();
                for (int i = 0; i < tempList.size(); i++) {
                    subList.add(tempList.get(i));
                }

                list.add(subList);

                tempList.clear();
            }

        } 

        return list;
    }
}

需要注意的地方

List的创建
ArrayList的操作不熟悉


剑指 Offer 32 - III. 从上到下打印二叉树 III

剑指 Offer 32 - III. 从上到下打印二叉树 III

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

例如:
给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

[
  [3],
  [20,9],
  [15,7]
]

提示:

    节点总数 <= 1000

思路

跟第二道题一样,只需要加入一个布尔值,判断当前需要是从左到右,还是从右到左打印即可。

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        //边界条件
        if (root == null) return new ArrayList<List<Integer>> ();

        //建立一个二维数组
        List<List<Integer>> list = new ArrayList<List<Integer>> ();

        //建立一个队列
        Queue<TreeNode> q = new LinkedList<TreeNode>();

        q.add(root);

        //队列中元素个数
        int queueCount = 1;

        List<Integer> tempList = new ArrayList<Integer> ();

        //是否是从左到右打印
        boolean leftToRight = true;

        while (!q.isEmpty()) {
            TreeNode tempNode = q.poll();
            tempList.add(tempNode.val);

            queueCount--;

            if (tempNode.left != null) {
                q.add(tempNode.left);
            }

            if (tempNode.right != null) {
                q.add(tempNode.right);
            }
            
            if (queueCount == 0) {
                queueCount = q.size();

                List<Integer> subList = new ArrayList<Integer> ();

                if (leftToRight) {
                    for (int i = 0; i < tempList.size(); i++) {
                        subList.add(tempList.get(i));
                    }
                }else{
                    for (int i = tempList.size() - 1; i >= 0; i--) {
                        subList.add(tempList.get(i));
                    }
                }

                leftToRight = !leftToRight;
                list.add(subList);

                tempList.clear();
            }

        } 

        return list;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值