JSFiddle - React, Tailwind, and code Playground
by Alex Myronov
JavaScript
const addLeftSubtree = (node, stack) => {
let curr = node
while (curr) {
stack.push(curr)
curr = curr.next
}
}
const postOrder = (node) => {
const stack = []
stack.push(node)
while (stack.length) {
let curr = stack[stack.length - 1]
if (curr.left) {
addLeftSubtree(curr, stack)
} else {
stack.pop()
addLeftSubtree(curr.right, stack)
console.log(curr.val, ':')
}
}
}
class Node {
constructor(val) {
this.val = val
this.left = null
this.right = null
}
}
const root = new Node("55")
root.left = new Node("35")
root.left.left = new Node("25")
root.left.left.left = new Node("15")
root.left.right = new Node("45")
root.right = new Node("65")
root.right.right = new Node("75")
root.right.right.left = new Node("87")
root.right.right.right = new Node("98")
console.log(postOrder(root))