Ask LSBot

Practice: Binary Tree Postorder Traversal

In this assignment, your task is to solve the "Postorder Traversal" problem with our final DFS pattern. Try solving the problem on your own using recursion.

Problem Description

// Given the root node of a binary tree, implement a
// function `postorderTraversal` that returns an
// array containing the values of the nodes visited in
// a postorder traversal.

class Node {
  constructor(value) {
    this.val = value;
    this.left = null;
    this.right = null;
  }
}

// Helper function for test cases
function buildTree(arr) {
  if (arr.length === 0) {
    return null;
  }

  const nodes = [];

  const val = arr.shift();
  const root = new Node(val);
  nodes.push(root);

  while (arr.length > 0) {
    const curr = nodes.shift();

    const leftVal = arr.shift();
    if (leftVal !== null) {
      curr.left = new Node(leftVal);
      nodes.push(curr.left);
    }

    if (arr.length > 0) {
      const rightVal = arr.shift();
      if (rightVal !== null) {
        curr.right = new Node(rightVal);
        nodes.push(curr.right);
      }
    }
  }

  return root;
}

// Test cases
const tree1 = buildTree([1, null, 2, 3]);
console.log(postorderTraversal(tree1)); // Output: [3, 2, 1]

const tree2 = buildTree([1, 2, 3, null, null, 4, null, null, 5]);
console.log(postorderTraversal(tree2)); // Output: [2, 5, 4, 3, 1]

const tree3 = buildTree([5, 3, null, 2, null, 1, null]);
console.log(postorderTraversal(tree3)); // Output: [1, 2, 3, 5]

const tree4 = buildTree([10, 5, 15, null, 6, 12, 21, null, null, 11]);
console.log(postorderTraversal(tree4)); // Output: [6, 5, 11, 12, 21, 15, 10]

Exercises

  1. Write a recursive definition for post-order traversal of a binary tree, including the base case and the recursive step.

    View Our Solution

    Base Case

    The base case is the same one as in the previous two problems:

    Base Case: Return from the function if node is null.

    Recursive Definition

    For the recursive definition in this problem, like in the previous problem, we have to follow a pattern, this time LRN.

    Recursive Definition: Perform a postorder traversal by first traversing current node's left child, then perform a postorder traversal on the current node's right child, and finally, add the node's value to the result array.

  2. Now implement the post-order traversal.

    View Our Solution

    function postorderTraversal(root) {
      const result = [];
    
      function traverse(node) {
        if (node === null) {
          return;
        }
        traverse(node.left);
        traverse(node.right);
        result.push(node.val);
      }
    
      traverse(root);
      return result;
    }
    
  3. Identify the time and space complexity of your post-order traversal.

    View Our Solution

    Time Complexity: O(N), where N is the number of nodes in the tree. Again, we need to visit each node exactly once.

    Space Complexity: O(h) for the recursive implementation due to the call stack, where h is the tree's height.

This conversation with LSBot is temporary. Sign up for free to save your conversations with LSBot.

Hi! I'm LSBot. I'm here to help you understand this chapter content with fast, focused answers.

Ask me about concepts, examples, or anything you'd like clarified from this chapter. I can explain complex topics, provide examples, or help connect ideas.

Want to know more? Refer to the LSBot User Guide.

This conversation with LSBot is temporary. Sign up for free to save your conversations with LSBot.

Hi! I'm LSBot. I'm here to help you think through this exercise by providing hints and guidance, without giving away the solution.

You can ask me about your approach, request clarification on the problem, or seek help when you feel stuck.

Want to know more? Refer to the LSBot User Guide.