var levelOrder = function(root) { if (root ==null){ return [] } let res = [] let resItem = [] let stack2 =[root] let stack1 = [] while(stack1.length!==0||stack2.length!==0){ while(stack2.length){ let node = stack2.pop() resItem.push(node.val) if (node.left){ stack1.push(node.left) } if (node.right){ stack1.push(node.right) } } res.push(resItem) resItem=[] while(stack1.length){ let node = stack1.pop() resItem.push(node.val) if (node.right){ stack2.push(node.right) } if (node.left){ stack2.push(node.left) } } if (resItem.length){ res.push(resItem) } resItem=[] } return res };