포스트 오더 순회

2480 단어 javascriptleetcode
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
};


좋은 웹페이지 즐겨찾기