Tree: Postorder Traversal

  • + 2 comments

    JavaScript Iterative Solution:

    function postOrder(root) {
        let str = '';
        let current = root;
        let stack = [];
        
        while (current != null) {
            str = ' ' + current.data + str;
            if (current.left != null) {
                stack.push(current.left);
            }
            if (current.right != null) {
                stack.push(current.right);
            }
            current = stack.pop();
        }
        
        process.stdout.write(str.substring(1));
    }