当前位置:网站首页>JS 力扣刷题 103. 二叉树的锯齿形层序遍历
JS 力扣刷题 103. 二叉树的锯齿形层序遍历
2022-04-23 13:54:00 【Candy_Rainbow_】
var zigzagLevelOrder = function(root) {
let rear = [];//遍历的队列
let answers = [];//二维数组
if(!root)return answers;//特殊情况
rear.push(root);//根节点入队
let flagStack = 1;//单层从左往右(队列)双层从右往左(栈)
while(rear.length > 0){
let answer = [];//一层一层遍历
let len = rear.length;//队列长度会变,记录以下
for(let i = 0; i < len; i++){
let tmp = rear.shift();
if(flagStack % 2 == 1)//单层从左往右(队列)
answer.push(tmp.val);
else//双层从右往左(栈)
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://blog.csdn.net/Candy_Rainbow_/article/details/124361996
边栏推荐
- Detailed explanation of constraints of Oracle table
- 聯想拯救者Y9000X 2020
- Function executes only the once function for the first time
- Dolphin scheduler configuring dataX pit records
- Scientists say Australian plan to cull up to 10,000 wild horses doesn’t go far enough
- Apache seatunnel 2.1.0 deployment and stepping on the pit
- Oracle alarm log alert Chinese trace and trace files
- PG SQL intercepts the string to the specified character position
- Android 面试主题集合整理
- SSM project deployed in Alibaba cloud
猜你喜欢
Android 面试主题集合整理
Lenovo Saver y9000x 2020
freeCodeCamp----time_ Calculator exercise
UML统一建模语言
Oracle defines self incrementing primary keys through triggers and sequences, and sets a scheduled task to insert a piece of data into the target table every second
Solution of discarding evaluate function in surprise Library
Oracle job scheduled task usage details
Oracle告警日志alert.log和跟踪trace文件中文乱码显示
MySQL and PgSQL time related operations
Detailed explanation of constraints of Oracle table
随机推荐
[code analysis (3)] communication efficient learning of deep networks from decentralized data
[code analysis (4)] communication efficient learning of deep networks from decentralized data
Oracle alarm log alert Chinese trace and trace files
Modification of table fields by Oracle
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Problems encountered in the project (V) understanding of operating excel interface poi
[code analysis (5)] communication efficient learning of deep networks from decentralized data
About note 1
Ora-16047 of a DG environment: dgid mismatch between destination setting and target database troubleshooting and listening vncr features
Parameter comparison of several e-book readers
Tensorflow Download
Detailed explanation of Oracle tablespace table partition and query method of Oracle table partition
自动化的艺术
UML统一建模语言
PG library to view the distribution keys of a table in a certain mode
try --finally
[code analysis (6)] communication efficient learning of deep networks from decentralized data
freeCodeCamp----arithmetic_ Arranger exercise
Window analysis function last_ VALUE,FIRST_ VALUE,lag,lead
第十五章 软件工程新技术