Skip to content

Commit

Permalink
0102二叉树的层序遍历javascript版本
Browse files Browse the repository at this point in the history
  • Loading branch information
xllpiupiu committed May 21, 2021
1 parent 0655161 commit d232e0f
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions problems/0102.二叉树的层序遍历.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,35 @@ public:
return result;
}
};
```
javascript代码:

```javascript
var levelOrder = function(root) {
//二叉树的层序遍历
let res=[],queue=[];
queue.push(root);
if(root===null){
return res;
}
while(queue.length!==0){
// 记录当前层级节点数
let length=queue.length;
//存放每一层的节点
let curLevel=[];
for(let i=0;i<length;i++){
let node=queue.shift();
curLevel.push(node.val);
// 存放当前层下一层的节点
node.left&&queue.push(node.left);
node.right&&queue.push(node.right);
}
//把每一层的结果放到结果数组
res.push(curLevel);
}
return res;
};

```

**此时我们就掌握了二叉树的层序遍历了,那么如下五道leetcode上的题目,只需要修改模板的一两行代码(不能再多了),便可打倒!**
Expand Down

0 comments on commit d232e0f

Please sign in to comment.