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)

πŸ” If you came here from Google, why not take a look at some of our top posts?