(树) 剑指 Offer 32 - III. 从上到下打印二叉树 III ——【Leetcode每日一题】

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

难度:中等

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

例如:

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

复制代码
    3
   / \
  9  20
    /  \
   15   7

返回其层次遍历结果:

复制代码
[
  [3],
  [20,9],
  [15,7]
]

提示

  • 节点总数 <= 1000

💡思路:BFS

解法和 剑指 Offer 32 - II. 从上到下打印二叉树 II 类似。

使用优先队列,唯一不同的是 返回值为「先从左往右,再从右往左」交替输出的锯齿形。

在这里设置有个反转的标签 re :

  • 每遍历一层 re 就取反;
  • re = true 时,则 temo 队列进行反转。

🍁代码:(C++、Java)

C++

cpp 复制代码
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> ans;
        if(root == nullptr) return ans;
        queue<TreeNode*> q;
        q.push(root);
        bool re = false;
        while(!q.empty()){
            vector<int> temp;
            int cnt = q.size();
            while(cnt-- > 0){
                TreeNode* cur = q.front();
                q.pop();
                temp.push_back(cur->val);
                if(cur->left != nullptr) q.push(cur->left);
                if(cur->right != nullptr) q.push(cur->right);
            }
            if(re) reverse(temp.begin(), temp.end());
            re = !re;
            ans.push_back(temp);
        }
        return ans;
    }
};

Java

java 复制代码
/**
 * 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) {
        List<List<Integer>> ans = new ArrayList<List<Integer>>();
        if(root == null) return ans;
        Queue<TreeNode> q = new LinkedList<>();
        q.add(root);
        boolean re = false;
        while(!q.isEmpty()){
            ArrayList<Integer> temp = new ArrayList<>();
            int cnt = q.size();
            while(cnt-- > 0){
                TreeNode cur = q.poll();
                temp.add(cur.val);
                if(cur.left != null) q.add(cur.left);
                if(cur.right != null) q.add(cur.right);
            }
            if(re) Collections.reverse(temp);
            re = !re;
            ans.add(temp);
        }
        return ans;
    }
}

🚀 运行结果:

🕔 复杂度分析:

  • 时间复杂度 : O ( n ) O(n) O(n),其中 n 为二叉树的节点数。每个节点会且仅会被遍历一次。
  • 空间复杂度 : O ( n ) O(n) O(n),我们需要维护存储节点的队列,队列中元素的个数不超过 n 个。

题目来源:力扣

放弃一件事很容易,每天能坚持一件事一定很酷,一起每日一题吧!
关注我LeetCode主页 / CSDN---力扣专栏,每日更新!

注: 如有不足,欢迎指正!

相关推荐
铉铉这波能秀39 分钟前
LeetCode Hot100数据结构背景知识之字典(Dictionary)Python2026新版
数据结构·python·算法·leetcode·字典·dictionary
我是咸鱼不闲呀1 小时前
力扣Hot100系列20(Java)——[动态规划]总结(下)( 单词拆分,最大递增子序列,乘积最大子数组 ,分割等和子集,最长有效括号)
java·leetcode·动态规划
唐梓航-求职中1 小时前
编程-技术-算法-leetcode-288. 单词的唯一缩写
算法·leetcode·c#
Ll13045252981 小时前
Leetcode二叉树part4
算法·leetcode·职场和发展
@––––––1 小时前
力扣hot100—系列4-贪心算法
算法·leetcode·贪心算法
im_AMBER2 小时前
Leetcode 115 分割链表 | 随机链表的复制
数据结构·学习·算法·leetcode
夏鹏今天学习了吗2 小时前
【LeetCode热题100(99/100)】柱状图中最大的矩形
算法·leetcode·职场和发展
sin_hielo3 小时前
leetcode 110
数据结构·算法·leetcode
老鼠只爱大米3 小时前
LeetCode经典算法面试题 #78:子集(回溯法、迭代法、动态规划等多种实现方案详细解析)
算法·leetcode·动态规划·回溯·位运算·子集
执着2593 小时前
力扣hot100 - 199、二叉树的右视图
数据结构·算法·leetcode