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:
Click - to see solution code
- C++
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;
}
};