当前位置:网站首页>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
边栏推荐
- Strange bug of cnpm
- freeCodeCamp----arithmetic_ Arranger exercise
- RAC environment error reporting ora-00239: timeout waiting for control file enqueue troubleshooting
- OSS cloud storage management practice (polite experience)
- Oracle database recovery data
- [machine learning] Note 4. KNN + cross validation
- 记录一个奇怪的bug:缓存组件跳转之后出现组件复制
- Test the time required for Oracle library to create an index with 7 million data in a common way
- Oracle clear SQL cache
- Atcoder beginer contest 248c dice sum (generating function)
猜你喜欢
Choreographer全解析
PG SQL intercepts the string to the specified character position
Search ideas and cases of large amount of Oracle redo log
Express ② (routage)
初探 Lambda Powertools TypeScript
Jenkins construction and use
Dolphin scheduler integrates Flink task pit records
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
低频量化之明日涨停预测
OSS cloud storage management practice (polite experience)
随机推荐
Oracle alarm log alert Chinese trace and trace files
[VMware] address of VMware Tools
AtCoder Beginner Contest 248C Dice Sum (生成函数)
elmo(BiLSTM-CRF+elmo)(Conll-2003 命名实体识别NER)
Port occupied 1
About note 1
Two ways to deal with conflicting data in MySQL and PG Libraries
Move blog to CSDN
Leetcode brush question 897 incremental sequential search tree
MySQL [read / write lock + table lock + row lock + mvcc]
Jenkins construction and use
Leetcode? The first common node of two linked lists
剑南春把文字游戏玩明白了
Apache seatunnel 2.1.0 deployment and stepping on the pit
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
PG SQL intercepts the string to the specified character position
Leetcode | 38 appearance array
[code analysis (3)] communication efficient learning of deep networks from decentralized data
About me
19c environment ora-01035 login error handling