【乱序版 ● 剑指offer】每日算法题打卡题解—— 从上到下打印二叉树(题号32)

打卡day4
题目往往不是我想的那么简单。。。
Orz

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

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

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

  3    
/   \   
9    20
     / \    
     15   7 

返回:

[3,9,20,15,7]

提示:

节点总数 <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof

解题思路:
将二叉树的 从上至下 打印(即按层打印),又称为二叉树的 广度优先搜索(BFS)。
BFS 通常借助 队列 的先入先出特性来实现。

java代码:

class Solution {
    public int[] levelOrder(TreeNode root) {
        if(root == null) {//当树的根节点为空时,返回空列表
        	return new int[0];
        }
        //建立包含根节点的队列
        Queue<TreeNode> queue = new LinkedList<>(){{ add(root); }};
        ArrayList<Integer> ans = new ArrayList<>();
        while(!queue.isEmpty()) {//当队列为空时,跳出循环
            TreeNode node = queue.poll();//队首元素出队,记为node
            ans.add(node.val);//将node。val添加至列表tmp尾部
            if(node.left != null) {//子节点不为空,加入队列
            	queue.add(node.left);
            }
            if(node.right != null) {
            	queue.add(node.right);
            }
        }
        int[] res = new int[ans.size()];
        for(int i = 0; i < ans.size(); i++)
            res[i] = ans.get(i);
        return res;
    }

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

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

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

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

  3    
/   \   
9    20
     / \    
     15   7 

返回其层次遍历结果:

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

提示:

节点总数 <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-ii-lcof

解题思路:
比上一题多了一个条件,每层打印到一行。相当于每一行的列表成了一个元素。
java代码:

    public List<List<Integer>> levelOrder(TreeNode root) {
    //定义队列列表
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        //根节点为空,返回空列表
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {//BFS循环
            List<Integer> tmp = new ArrayList<>();//新建临时列表,用于存储当前层的打印结果
            for(int i = queue.size(); i > 0; i--) {//i为层节点数数,循环次数为当前层节点数
                TreeNode node = queue.poll();//出队
                tmp.add(node.val);
                //添加子节点
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            res.add(tmp);
        }
        return res;
    }
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */

第三题:Offer 32 - III. 从上到下打印二叉树 III

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

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

  3    
/   \   
9    20
     / \    
     15   7 

返回其层次遍历结果:

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

提示:

节点总数 <= 1000

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-iii-lcof

解题思路:
第一题主要考察 树的按层打印 ;
第二题要求 每一层打印到一行 ;
第三题要求 打印顺序交替变化
需要判断一下奇偶层,在偶数层采用倒序操作。

java代码:

    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<TreeNode> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();
        if(root != null) queue.add(root);
        while(!queue.isEmpty()) {
            List<Integer> tmp = new ArrayList<>();
            for(int i = queue.size(); i > 0; i--) {
                TreeNode node = queue.poll();
                tmp.add(node.val);
                if(node.left != null) queue.add(node.left);
                if(node.right != null) queue.add(node.right);
            }
            if(res.size() % 2 == 1) Collections.reverse(tmp);//偶数层倒序操作
            res.add(tmp);
        }
        return res;
    }



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

寂静花开

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值