const bt = { val: 1, left: { val: 2, left: { val: 4, left: null, right: null, }, right: { val: 5, left: null, right: null, } }, right: { val: 3, left: { val: 6, left: null, right: null, }, right: { val: 7, left: null, right: null, } }}
//先序遍历const preorder = (root) => { if (!root) { return } const stack = [root]; while (stack.length > 0) { const n = stack.pop() console.log(n.val) if (n.right) stack.push(n.right) if (n.left) stack.push(n.left) }}

//中序遍历const inorder = (root) => { if (!root) return const stack = []; let p = root; while (stack.length || p) { while (p) { stack.push(p); p = p.left } const n = stack.pop() console.log(n.val) p = n.right }}

//后序遍历const postorder = (root) => { if (!root) return const stack = [root] const res = [] while (stack.length) { const n = stack.pop() res.push(n) if (n.left) stack.push(n.left) if (n.right) stack.push(n.right) } while(res.length){ const n = res.pop() console.log(n.val) }}