当前位置:网站首页>[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
[LeetCode Daily Question] - 103. Zigzag Level Order Traversal of Binary Tree
2022-08-02 02:00: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
七【解题思路】
- The level-order traversal is definitely usedBFS(广度优先搜索)
- Based on this idea, level-order traversal is performed,It is relatively simple for layer-order traversal,Simply scan the elements of this layer, pop them from the queue and add them to the result array
- But the problem requires that the left and right order of storage is constantly reversed,So set a flag bit,Initially stored from left to right,Then after each layer is stored,修改标志位,Let it store right to left.By constantly modifying the flag bits, the layer sequence storage with different left and right orders is achieved
八【时间频度】
- 时间复杂度: 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语言版
边栏推荐
- libcurl访问url保存为文件的简单示例
- 【ORB_SLAM2】void Frame::ComputeImageBounds(const cv::Mat &imLeft)
- The characteristics and principle of typescript29 - enumeration type
- oracle查询扫描全表和走索引
- LeetCode brushing diary: 53, the largest sub-array and
- 飞桨助力航天宏图PIE-Engine地球科学引擎构建
- Garbage Collector CMS and G1
- 6-24漏洞利用-vnc密码破解
- LeetCode刷题日记:LCP 03.机器人大冒险
- 哈希冲突和一致性哈希
猜你喜欢
Kubernetes之本地存储
数据链路层的数据传输
秒懂大模型 | 3步搞定AI写摘要
pcie inbound和outbound关系
Hash collisions and consistent hashing
hash table
Typescript31 - any type
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
The characteristics and principle of typescript29 - enumeration type
2023年起,这些地区软考成绩低于45分也能拿证
随机推荐
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
LeetCode Review Diary: 153. Find the Minimum Value in a Rotated Sort Array
Garbage Collector CMS and G1
项目后台技术Express
Shell Beginners Final Chapter
编码经验之谈
AntPathMatcher使用
搜罗汇总的效应
When paying attention to the "Internet +" model, you usually only focus on the "Internet +" model itself
¶Backtop 回到顶部 不生效
【LeetCode每日一题】——654.最大二叉树
JDBC PreparedStatement 的命名参数实现
PHP 使用 PHPRedis 与 Predis
3.Bean的作用域与生命周期
The characteristics and principle of typescript29 - enumeration type
【LeetCode每日一题】——704.二分查找
HSDC和独立生成树相关
MySQL8 下载、启动、配置、验证
【LeetCode Daily Question】——704. Binary Search
typescript31-any类型