const traversePostOrderIterative = (root) => {
  let result = []

  // Create two stacks
  var s1 = [],
    s2 = []

  // Push root to first stack
  s1.push(root)

  //# Run while first stack is not empty
  while (s1.length) {
    // Pop an item from s1 and append it to s2
    var node = s1.pop()
    s2.push(node)

    // Push left and right children of removed item to s1
    if (node.left) s1.push(node.left)
    if (node.right) s1.push(node.right)
  }
  // Print all eleements of second stack
  while (s2.length) {
    var node = s2.pop()
    result.push(node.val)
  }

  return result
}

module.exports = traversePostOrderIterative
