当前位置:网站首页>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
边栏推荐
- 自动化的艺术
- 项目中遇到的问题(五)操作Excel接口Poi的理解
- Oracle generates millisecond timestamps
- About note 1
- Storage scheme of video viewing records of users in station B
- AttributeError: ‘dict‘ object has no attribute ‘iteritems‘
- Influence of openssh version on SSH mutual trust creation in RAC environment
- AtCoder Beginner Contest 248C Dice Sum (生成函数)
- Two ways to deal with conflicting data in MySQL and PG Libraries
- Express②(路由)
猜你喜欢
JUC interview questions about synchronized, ThreadLocal, thread pool and atomic atomic classes
SQL learning | set operation
【报名】TF54:工程师成长地图与卓越研发组织打造
About note 1
MySQL and PgSQL time related operations
服务器中挖矿病毒了,屮
Leetcode | 38 appearance array
Leetcode brush question 𞓜 13 Roman numeral to integer
美联储数字货币最新进展
2021年秋招,薪资排行NO
随机推荐
[code analysis (4)] communication efficient learning of deep networks from decentralized data
Using Jupiter notebook in virtual environment
服务器中挖矿病毒了,屮
神经元与神经网络
Ora-16047 of a DG environment: dgid mismatch between destination setting and target database troubleshooting and listening vncr features
crontab定时任务输出产生大量邮件耗尽文件系统inode问题处理
Dolphin scheduler integrates Flink task pit records
redis如何解决缓存雪崩、缓存击穿和缓存穿透问题
OSS cloud storage management practice (polite experience)
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Multithreading
Express②(路由)
Lenovo Saver y9000x 2020
Choreographer全解析
Oracle database recovery data
Tensorflow Download
Dynamic subset division problem
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
cnpm的诡异bug
Oracle job scheduled task usage details