# HackerRank – Tree: Postorder Traversal Solution in JavaScript

Tree: Postorder Traversal is a coding challenge with easy difficulty in the HackerRank data structures category. In this blog post, we’ll discuss how we can solve it in JavaScript in O(n) time and O(1) space.

## Problem Statement

Complete the postOrder function in the editor below. It received  parameter: a pointer to the root of a binary tree. It must print the values in the tree’s postorder traversal as a single line of space-separated values.

Read full details and access the challenge on Tree: Postorder Traversal | HackerRank

## Solution

``````function postOrder(root) {
if (root) {
postOrder(root.left);
postOrder(root.right);
process.stdout.write(root.data + ' ');
}
}``````

Time Complexity : O(n)

Space Complexity : O(1)

The solution to this challenge is very similar to the previous challenge, HackerRank – Tree: Preorder Traversal Solution in JavaScript.

The difference is that we print the data after we call the functions. This will cause the data to be printed from the end.

We’re visiting all the nodes and use no extra data structure so we have O(n) time and O(1) space complexity.

## Conclusion

That’s how you can solve the Tree: Postorder Traversal Challenge in HackerRank.

This is a basic skill on trees and it is a nice challenge to improve our skill.

If you have another approach different from mine, please do comment below!

Check out the rest of my blog for more helpful contents on Data Structures and Algorithms in JavaScript! We’ll also discuss more HackerRank solutions in JavaScript for the upcoming posts!

See you next post!

### 1 thought on “HackerRank – Tree: Postorder Traversal Solution in JavaScript”

1. Ꮃe are a group of volunteers and opening a new scheme in our community.
Your website provided us with valuable info to woгk on. You һave done a formidable
job and our whole community will be gгateful to
you.