public static List<Integer> postorderTraversal(TreeNode<Integer> root) {
// Write your code here
Stack<TreeNode<Integer>> stack1 = new Stack<>();
Set<TreeNode<Integer>> visited = new HashSet<>();
List<Integer> traversals = new ArrayList<>();
stack1.push(root);
while(!stack1.isEmpty()){
TreeNode<Integer> topTreeNode = stack1.peek();
if(!visited.contains(topTreeNode)){
visited.add(topTreeNode);
if(null != topTreeNode.right){
stack1.push(topTreeNode.right);
}
if(null != topTreeNode.left){
stack1.push(topTreeNode.left);
}
}else{
TreeNode<Integer> poppedNode = stack1.pop();
traversals.add(poppedNode.data);
}
}
return traversals;
}