Skip to content

Instantly share code, notes, and snippets.

@vamsitallapudi
Created December 23, 2020 02:48
Show Gist options
  • Save vamsitallapudi/a64a28b90d1e2c05c3ecef5e92bef4af to your computer and use it in GitHub Desktop.
Save vamsitallapudi/a64a28b90d1e2c05c3ecef5e92bef4af to your computer and use it in GitHub Desktop.
import java.util.ArrayDeque
import java.util.LinkedList
fun postOrderIterative(root: TreeNode?): List<Int> {
if(root == null) return emptyList()
val stack = ArrayDeque<TreeNode>()
val list = LinkedList<Int>()
stack.push(root)
while (stack.isNotEmpty()) {
val node = stack.pop()
list.addFirst(node.data)
node.left?.let { stack.push(it) }
node.right?.let { stack.push(it) }
}
return list
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment