Open In App

JavaScript Program for Postorder Traversal of Binary Tree

In a binary tree, a node always has its two children – left and right children. The left and right children of any node can be another node or may be NULL. The postorder traversal of a binary tree is traversing the root in the following manner – first the left child node, then the right child node, and at last the main node is being traversed. We will traverse in the following manner until the NULL node arrives.

Example:

Postorder Traversal of Binary Tree

Postorder traversal result of above binary tree shown in the image is: 4 5 2 1 6 7 3



Recursive Approach

In recursive approach , we make a recursive call to traverse the binary tree in postorder manner. First we call the left node, then right node and in last we call the main node on which the pointer rest. Recursively call the left child node, then call the right child node and in last call the node itself.



Example : The below code traverse the binary tree in postorder manner using recursion.




class Node {
    constructor(data) {
        this.data = data;
        this.left = null;
        this.right = null;
    }
}
 
function postOrderTraversal(curr, postOrder) {
    if (curr === null) return;
 
    postOrderTraversal(curr.left, postOrder);
    postOrderTraversal(curr.right, postOrder);
    postOrder.push(curr.data);
}
 
function newNode(data) {
    let node = new Node();
    node.data = data;
    node.left = null;
    node.right = null;
    return node;
}
 
let root = newNode(3);
root.left = newNode(5);
root.right = newNode(4);
root.left.left = newNode(8);
root.left.right = newNode(9);
root.left.right.left = newNode(10);
root.right.left = newNode(16);
root.right.right = newNode(20);
root.right.right.left = newNode(13);
root.right.right.right = newNode(14);
 
let postOrder = [];
postOrderTraversal(root, postOrder);
 
console.log("The postOrder Traversal of above tree is  " +
postOrder.join(" "));

Output
The postOrder Traversal of above tree is  8 10 9 5 16 13 14 20 4 3

Iterative Approach

In iterative approach, we perform the postorder traversal of Binary tree using stack. It is similar to above approach, the only difference is we traverse the binary tree using stack in iterative manner. Use stack to store the node. Pop a node from the stack and add the value of the popped node to the postorder stack. Then, push the left child node in the stack and then push the right child node in the stack. When traversal is completed reverse the postorder stack to get postorder traversal.

Example : The below code uses the iterative approach to traverse the binary tree in postorder manner.




class TreeNode {
    constructor(val) {
        this.val = val;
        this.left = null;
        this.right = null;
    }
}
 
function postorderTraversal(root) {
    const result = [];
    if (!root) return result;
 
    const stack = [];
    let prev = null;
 
    do {
        while (root !== null) {
            stack.push(root);
            root = root.left;
        }
 
        while (root === null && stack.length > 0) {
            root = stack[stack.length - 1];
            if (root.right === null ||
                root.right === prev) {
                result.push(root.val);
                stack.pop();
                prev = root;
                root = null;
            } else {
                root = root.right;
            }
        }
    } while (stack.length > 0);
 
    return result;
}
 
 
const root = new TreeNode(5);
root.left = new TreeNode(7);
root.right = new TreeNode(13);
root.left.left = new TreeNode(10);
root.left.right = new TreeNode(8);
root.left.left.left = new TreeNode(16);
root.left.left.right = new TreeNode(18);
 
const postorder =
    postorderTraversal(root);
console.log
    (" The Postorder Traversal of above binary tree is ",
        postorder.join(" "));

Output
 The Postorder Traversal of above binary tree is  16 18 10 8 7 13 5

Article Tags :