DEV Community 👩‍💻👨‍💻

ZeeshanAli-0704
ZeeshanAli-0704

Posted on

Post Order Traversal

var postOrderTraversal = function (root) {
  // post Order Traversal ->
  //  1. Recursively traverse through the left subtree
  //  2. Recursively traverse through the right subtree
  //  3. Visit current node

  // Initialize array of values
  let result = [];

  // Recursive function to traverse through subtrees
  travel(root, result);

  return result;
};

const travel = (node, result) => {
  if (node === null) {
    return null;
  }
  result.push(node.val); // Visit node
  travel(node.left, result); // Traverse through left subtree
  travel(node.right, result); // Traverse through right subtree
};


Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Need a better mental model for async/await?

Check out this classic DEV post on the subject.

⭐️🎀 JavaScript Visualized: Promises & Async/Await

async await