当前位置:网站首页>【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
【LeetCode每日一题】——103.二叉树的锯齿形层序遍历
2022-08-02 01:57:00 【IronmanJay】
一【题目类别】
- 二叉树
二【题目难度】
- 中等
三【题目编号】
- 103.二叉树的锯齿形层序遍历
四【题目描述】
- 给你二叉树的根节点 root ,返回其节点值的 锯齿形层序遍历 。(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行)。
五【题目示例】
示例 1:
输入:root = [3,9,20,null,null,15,7]
输出:[[3],[20,9],[15,7]]示例 2:
输入:root = [1]
输出:[[1]]示例 3:
输入:root = []
输出:[]
六【题目提示】
- 树中节点数目在范围 [0, 2000] 内
- -100 <= Node.val <= 100
七【解题思路】
- 层序遍历肯定使用的是BFS(广度优先搜索)
- 基于这个思路进行层序遍历,对于层序遍历比较简单,直接扫描这一层的元素在队列弹出然后加入到结果数组中即可
- 但是题目要求存储的左右顺序不停的反转,所以设置一个标志位,一开始从左到右存储,然后每存储完一层之后,修改标志位,让其从右到左存储。通过不停的修改标志位来达到左右顺序不同的层序存储
八【时间频度】
- 时间复杂度: O ( n ) O(n) O(n),其中 n n n为树的节点个数
- 空间复杂度: O ( n ) O(n) O(n),其中 n n n为树的节点个数
九【代码实现】
- Java语言版
package Tree;
import java.util.*;
public class p103_ZigzagOrderTraversalOfBinaryTrees {
int val;
p103_ZigzagOrderTraversalOfBinaryTrees left;
p103_ZigzagOrderTraversalOfBinaryTrees right;
public p103_ZigzagOrderTraversalOfBinaryTrees() {
}
public p103_ZigzagOrderTraversalOfBinaryTrees(int val) {
this.val = val;
}
public p103_ZigzagOrderTraversalOfBinaryTrees(int val, p103_ZigzagOrderTraversalOfBinaryTrees left, p103_ZigzagOrderTraversalOfBinaryTrees right) {
this.val = val;
this.left = left;
this.right = right;
}
public static void main(String[] args) {
p103_ZigzagOrderTraversalOfBinaryTrees root = new p103_ZigzagOrderTraversalOfBinaryTrees(3);
p103_ZigzagOrderTraversalOfBinaryTrees left = new p103_ZigzagOrderTraversalOfBinaryTrees(9);
p103_ZigzagOrderTraversalOfBinaryTrees right = new p103_ZigzagOrderTraversalOfBinaryTrees(20);
p103_ZigzagOrderTraversalOfBinaryTrees right1 = new p103_ZigzagOrderTraversalOfBinaryTrees(15);
p103_ZigzagOrderTraversalOfBinaryTrees right2 = new p103_ZigzagOrderTraversalOfBinaryTrees(7);
root.left = left;
root.right = right;
right.left = right1;
right.right = right2;
List<List<Integer>> res = zigzagLevelOrder(root);
System.out.println("res = " + res);
}
public static List<List<Integer>> zigzagLevelOrder(p103_ZigzagOrderTraversalOfBinaryTrees root) {
List<List<Integer>> res = new LinkedList<List<Integer>>();
boolean flag = true;
if (root == null) {
return res;
}
Queue<p103_ZigzagOrderTraversalOfBinaryTrees> queue = new ArrayDeque<p103_ZigzagOrderTraversalOfBinaryTrees>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
Deque<Integer> list = new LinkedList<Integer>();
for (int i = 0; i < size; i++) {
p103_ZigzagOrderTraversalOfBinaryTrees temp = queue.poll();
if (flag) {
list.offerLast(temp.val);
} else {
list.offerFirst(temp.val);
}
if (temp.left != null) {
queue.offer(temp.left);
}
if (temp.right != null) {
queue.offer(temp.right);
}
}
res.add(new LinkedList<Integer>(list));
if (flag) {
flag = false;
} else {
flag = true;
}
}
return res;
}
}
- C语言版
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct TreeNode
{
int val;
struct TreeNode *left;
struct TreeNode *right;
};
int** zigzagLevelOrder(struct TreeNode* root, int* returnSize, int** returnColumnSizes)
{
int** res = (int**)malloc(sizeof(int*) * 2001);
*returnColumnSizes = (int*)malloc(sizeof(int) * 2001);
*returnSize = 0;
struct TreeNode* queue[2001];
int front = 0;
int rear = 0;
bool flag = true;
if (root == NULL)
{
(*returnColumnSizes)[*returnSize] = 0;
return res;
}
queue[rear++] = root;
while (front != rear)
{
res[*returnSize] = (int*)malloc(sizeof(int) * 2001);
(*returnColumnSizes)[*returnSize] = 0;
int size = (rear - front + 2001) % 2001;
int count = 0;
for (int i = 0; i < size; i++)
{
struct TreeNode* temp = queue[front++];
if (flag)
{
res[*returnSize][count] = temp->val;
}
else
{
res[*returnSize][size - count - 1] = temp->val;
}
if (temp->left != NULL)
{
queue[rear++] = temp->left;
}
if (temp->right != NULL)
{
queue[rear++] = temp->right;
}
count++;
}
(*returnColumnSizes)[*returnSize] = count;
*returnSize = *returnSize + 1;
if (flag)
{
flag = false;
}
else
{
flag = true;
}
}
return res;
}
/*主函数省略*/
十【提交结果】
Java语言版
C语言版
边栏推荐
猜你喜欢
秒懂大模型 | 3步搞定AI写摘要
检查IP或端口是否被封
Understand the big model in seconds | 3 steps to get AI to write a summary
Some insights from 5 years of automated testing experience: UI automation must overcome these 10 pits
用位运算为你的程序加速
YGG 公会发展计划第 1 季总结
手写博客平台~第二天
After graduating from three books, I was rejected by Tencent 14 times, and finally successfully joined Alibaba
【图像融合】基于加权和金字塔实现图像融合附matlab代码
垃圾回收器CMS和G1
随机推荐
牛顿定理和相关推论
6-24漏洞利用-vnc密码破解
LeetCode刷题日记: 33、搜索旋转排序数组
Golang分布式应用之Redis
Redis Subscription and Redis Stream
成都openGauss用户组招募啦!
秒懂大模型 | 3步搞定AI写摘要
LeetCode Brushing Diary: 74. Searching 2D Matrix
手写一个博客平台~第三天
用位运算为你的程序加速
AntPathMatcher使用
3. Bean scope and life cycle
浅谈国产ERP的“横纵竖”三向发展态势
制造企业数字化转型现状分析
MySQL优化策略
【图像融合】基于加权和金字塔实现图像融合附matlab代码
Yunhe Enmo: Let the value of the commercial database era continue to prosper in the openGauss ecosystem
力扣 1374. 生成每种字符都是奇数个的字符串
For effective automated testing, these software testing tools must be collected!!!
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided