当前位置:网站首页>JS force deduction brush question 103 Zigzag sequence traversal of binary tree
JS force deduction brush question 103 Zigzag sequence traversal of binary tree
2022-04-23 13:56:00 【Candy_ Rainbow_】
var zigzagLevelOrder = function(root) {
let rear = [];// Traversal queue
let answers = [];// Two dimensional array
if(!root)return answers;// A special case
rear.push(root);// Root in line
let flagStack = 1;// Single layer from left to right ( queue ) Double deck from right to left ( Stack )
while(rear.length > 0){
let answer = [];// Traversal layer by layer
let len = rear.length;// The queue length will change , Record the following
for(let i = 0; i < len; i++){
let tmp = rear.shift();
if(flagStack % 2 == 1)// Single layer from left to right ( queue )
answer.push(tmp.val);
else// Double deck from right to left ( Stack )
answer.unshift(tmp.val);
if(tmp.left)rear.push(tmp.left);
if(tmp.right)rear.push(tmp.right);
}
answers.push(answer);
flagStack++;
}
return answers;
};
版权声明
本文为[Candy_ Rainbow_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231353588755.html
边栏推荐
猜你喜欢
Quartus Prime硬件实验开发(DE2-115板)实验一CPU指令运算器设计
SSM project deployed in Alibaba cloud
[VMware] address of VMware Tools
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
Detailed explanation of redis (Basic + data type + transaction + persistence + publish and subscribe + master-slave replication + sentinel + cache penetration, breakdown and avalanche)
The query did not generate a result set exception resolution when the dolphin scheduler schedules the SQL task to create a table
Express②(路由)
Jenkins construction and use
零拷貝技術
解决方案架构师的小锦囊 - 架构图的 5 种类型
随机推荐
Haruki Murakami -- Excerpt from "what do I talk about when I talk about running"
SSM project deployed in Alibaba cloud
Oracle modify default temporary tablespace
leetcode--977. Squares of a Sorted Array
零拷貝技術
[code analysis (3)] communication efficient learning of deep networks from decentralized data
Oracle clear SQL cache
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Multithreading
Dolphin scheduler integrates Flink task pit records
【报名】TF54:工程师成长地图与卓越研发组织打造
Quartus Prime硬件实验开发(DE2-115板)实验二功能可调综合计时器设计
Technologie zéro copie
Tensorflow & pytorch common error reporting
Use future and countdownlatch to realize multithreading to execute multiple asynchronous tasks, and return results after all tasks are completed
【项目】小帽外卖(八)
Kettle--控件解析
Information: 2021 / 9 / 29 10:01 - build completed with 1 error and 0 warnings in 11S 30ms error exception handling
村上春树 --《当我谈跑步时,我谈些什么》句子摘录
The art of automation