一、题目
Given a binary tree, return the bottom-up level order traversal of its nodes’ values. (ie, from left to right, level by level from leaf to root)。
二、题目大意
给定二叉树,返回其节点值的自下而上的顺序遍历。(即从左到右,从叶到根逐级排列)。
三、解题思路
使用队列的思想。
四、代码实现
const levelOrderBottom = root => {
if (!root) {
return []
}
const queue = [root]
const ans = []
while (queue.length) {
const temp = []
const size = queue.length
for (let i = 0; i < size; i++) {
const item = queue.pop()
if (item) {
temp.push(item.val)
if (item.left) {
queue.unshift(item.left)
}
if (item.right) {
queue.unshift(item.right)
}
}
}
ans.unshift(temp)
}
return ans
}
如果本文对您有帮助,欢迎关注微信公众号,为您推送更多大前端相关的内容, 欢迎留言讨论,ε=ε=ε=┏(゜ロ゜;)┛。

您还可以在这些地方找到我: