function f(root) { let re = []; let stack = []; if(root !== null) { stack.push(root); } while(stack.length) { let node = stack.pop(); re.push(node.val); if(node.right) { stack.push(node.right); } if(node.left) { stack.push(node.left); } } return re; } var root = { val: 1, left: { val: 2, left: { val: 4, }, right:{ val:5 } }, right: { val: 3, left: { val: 6 }, right: { val: 7 } } } console.log(f(root));