diff --git a/problems/0102.二叉树的层序遍历.md b/problems/0102.二叉树的层序遍历.md index 0ab67b13..1cb4164f 100644 --- a/problems/0102.二叉树的层序遍历.md +++ b/problems/0102.二叉树的层序遍历.md @@ -687,6 +687,26 @@ func levelOrder(root *TreeNode) [][]int { 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)