We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Tree: Postorder Traversal
Tree: Postorder Traversal
Sort by
recency
|
233 Discussions
|
Please Login in order to post a comment
void postOrder(Node *root) { if (root == NULL) { return; // Base case: if node is null, just return } postOrder(root->left); // Traverse left subtree postOrder(root->right); // Traverse right subtree cout << root->data << " "; // Visit the root node (print data) }
My Java solution with o(n) time complexity and o(h) space complexity:
Python
Very Simple Recursive JavaScript Solution:
const resp = []
function helperFunc(root) { if(!root) return; resp.push(root.data) helperFunc(root.right); helperFunc(root.left); }
function postOrder(root) { helperFunc(root); console.log(resp.reverse().join(' ')); }
It is asking how to use process.stdout.write to print your answer on JS, not testing the Tree concept.