mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 21:10:58 +08:00
添加 “102.二叉树的层序遍历”Javascript版本
This commit is contained in:
@ -687,6 +687,26 @@ func levelOrder(root *TreeNode) [][]int {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
Javascript:
|
||||||
|
```javascript
|
||||||
|
var levelOrder = function (root) {
|
||||||
|
let ans = [];
|
||||||
|
if (!root) return ans;
|
||||||
|
let queue = [root];
|
||||||
|
while (queue.length) {
|
||||||
|
let size = queue.length;
|
||||||
|
let temp = [];
|
||||||
|
while (size--) {
|
||||||
|
let n = queue.shift();
|
||||||
|
temp.push(n.val);
|
||||||
|
if (n.left) queue.push(n.left);
|
||||||
|
if (n.right) queue.push(n.right);
|
||||||
|
}
|
||||||
|
ans.push(temp);
|
||||||
|
}
|
||||||
|
return ans;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user