题解 | #从上往下打印二叉树#
从上往下打印二叉树
https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function PrintFromTopToBottom(root) { // write code here const res = [] if (root === null) return res const stack = [root] while (stack.length > 0) { const currentNode = stack.shift() res.push(currentNode.val) if (currentNode.left) stack.push(currentNode.left) if (currentNode.right) stack.push(currentNode.right) } return res } module.exports = { PrintFromTopToBottom : PrintFromTopToBottom };