当前位置:网站首页>[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语言版
边栏推荐
- Chengdu openGauss user group recruit!
- 乱七八糟的网站
- 6-24漏洞利用-vnc密码破解
- typescript29-枚举类型的特点和原理
- bool Frame::PosInGrid(const cv::KeyPoint &kp, int &posX, int &posY)
- Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021:解读
- 【服务器数据恢复】服务器Raid5阵列mdisk磁盘离线的数据恢复案例
- Force buckle, 752-open turntable lock
- 『网易实习』周记(二)
- HSDC is related to Independent Spanning Tree
猜你喜欢
【LeetCode Daily Question】——704. Binary Search
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
LeetCode刷题日记:LCP 03.机器人大冒险
MySQL8 下载、启动、配置、验证
Byte taught me a hard lesson: When a crisis comes, you don't even have time to prepare...
Constructor instance method inheritance of typescript37-class (extends)
Multi-Party Threshold Private Set Intersection with Sublinear Communication-2021: Interpretation
字节给我狠狠上了一课:危机来的时候你连准备时间都没有...
飞桨助力航天宏图PIE-Engine地球科学引擎构建
3.Bean的作用域与生命周期
随机推荐
Handwritten Blog Platform ~ Day Two
Day115. Shangyitong: Background user management: user lock and unlock, details, authentication list approval
kubernetes之服务发现
力扣、752-打开转盘锁
A full set of common interview questions for software testing functional testing [open thinking questions] interview summary 4-3
力扣(LeetCode)213. 打家劫舍 II(2022.08.01)
Check if IP or port is blocked
ofstream,ifstream,fstream读写文件
Redis Persistence - RDB and AOF
hash table
Entry name ‘org/apache/commons/codec/language/bm/gen_approx_greeklatin.txt’ collided
3. Bean scope and life cycle
云和恩墨:让商业数据库时代的价值在openGauss生态上持续繁荣
Image fusion based on weighted 】 and pyramid image fusion with matlab code
LeetCode brush diary: LCP 03. Machine's adventure
typescript32-ts中的typeof
Fundamentals of Cryptography: X.690 and Corresponding BER CER DER Encodings
《自然语言处理实战入门》 基于知识图谱的问答机器人
Understand the big model in seconds | 3 steps to get AI to write a summary
Kubernetes之本地存储