You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
var levelOrder = function(root) {
if(!root || root.val == null) return []
let queue = []
let res = []
queue.push(root)
while(queue.length){
let len = queue.length
let curr = []
for(var i =0;i<len;i++){
let node = queue.shift()
curr.push(node.val)
if(node.left)queue.push(node.left)
if(node.right)queue.push(node.right)
}
res.push(curr)
}
return res
};
Activity
plane-hjh commentedon May 21, 2020
这道题和二叉树的层次遍历相似,只需要把
reverse()去除就可以了广度优先遍历
深度优先遍历
AnranS commentedon Nov 19, 2020
lewisYe commentedon Apr 9, 2021
使用广度优先遍历 然后再对数据进行分层