Skip to main content

Binary Tree Postorder Traversal

Problem

Given the root of a binary tree, return the postorder traversal of its nodes' values.

Solution Approach

Expected Time complexity: O(n)O(n)

Click - to see solution code
class Solution {
vector<int> postorder;

public:
void traversal(TreeNode* root) {
if (!root) return;
traversal(root->left);
traversal(root->right);
postorder.push_back(root->val);
}

vector<int> postorderTraversal(TreeNode* root) {
traversal(root);
return postorder;
}
};