-1

I have tried to use a Global Array to save the values. But how do I write the base case for the recursion? If I return an array each time, how do I maintain the correct order of the tree traversal?

1 Answers1

0

Not sure what language you're using, but since you mentioned trying a global array I'm assuming it allows you to append to an array. This means you don't really care about tracking the right array index for each tree value since, as long as you append to the array in the right order. Something like the following pseudo-code will work.

postOrderTree: int[] = [];

func createPostOrderTree(TreeNode node) {
  if node is null {
    return;
  }
  createPostOrderTree(node.leftChild);
  createPostOrderTree(node.rightChild);
  postOrderTree.append(node.val);
}
vball
  • 359
  • 1
  • 14