Given the root of a&nbsp;binary tree, return the postorder traversal of its nodes' values.

&nbsp;
Example 1:

Input: root = [1,null,2,3]
Output: [3,2,1]


Example 2:

Input: root = []
Output: []


Example 3:

Input: root = [1]
Output: [1]


&nbsp;
Constraints:


	The number of the nodes in the tree is in the range [0, 100].
	-100 &lt;= Node.val &lt;= 100


&nbsp;
Follow up: Recursive solution is trivial, could you do it iteratively?
from typing import List, Optional


class Solution:
	"""
	Time:   O(n)
	"""

	def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
		if root is None:
			return []

		postorder = []
		stack = [root]

		while stack:
			node = stack.pop()
			postorder.append(node.val)
			if node.left is not None:
				stack.append(node.left)
			if node.right is not None:
				stack.append(node.right)

		return postorder[::-1]


class Solution:
	"""
	Time:   O(n)
	"""

	def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
		return list(self.postorder_generator(root))

	@classmethod
	def postorder_generator(cls, tree: Optional[TreeNode]):
		if tree is not None:
			yield from cls.postorder_generator(tree.left)
			yield from cls.postorder_generator(tree.right)
			yield tree.val
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 * int val;
 * TreeNode left;
 * TreeNode right;
 * TreeNode() {}
 * TreeNode(int val) { this.val = val; }
 * TreeNode(int val, TreeNode left, TreeNode right) {
 * this.val = val;
 * this.left = left;
 * this.right = right;
 * }
 * }
 */
class Solution {
    List<Integer> res = new ArrayList<>();
    public List<Integer> postorderTraversal(TreeNode root) {
        traversal(root);
        return res;
    }

    public void traversal(TreeNode root){
        if(root == null)
            return;
        traversal(root.left);
        traversal(root.right);
        res.add(root.val);
    }
}
class Solution {
    void solve(TreeNode *root, vector<int> &ans){
        if(root == NULL) return;
        solve(root->left, ans);
        solve(root->right, ans);
        ans.push_back(root->val);
    }
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> ans;
        solve(root, ans);
        return ans;
    }
};
class Pair{
    constructor(node, state){
        this.node = node;
        this.state = state;
    }
}
var postorderTraversal = function(root) {
    let ans = [];
    let st = []; // stack
    root != null && st.push(new Pair(root, 1));

    while(st.length > 0){
        let top = st[st.length - 1];

        if(top.state == 1){

            top.state++;
            if(top.node.left != null){
                st.push(new Pair(top.node.left, 1))
            }
        } else if(top.state == 2){
            top.state++;
            if(top.node.right != null){
                st.push(new Pair(top.node.right, 1))
            }
        } else{
            ans.push(top.node.val);
            st.pop();
        }
    }
    return ans;
};
Binary Tree Postorder Traversal