09. Postorder Traversal
β GFG solution to the Postorder Traversal problem: traverse binary tree in postorder using Morris Traversal technique with O(1) space complexity. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given the root of a Binary Tree, your task is to return its Postorder Traversal.
A postorder traversal first visits the left child (including its entire subtree), then visits the right child (including its entire subtree), and finally visits the node itself.
π Examples
Example 1
Input: root = [19, 10, 8, 11, 13]
Output: [11, 13, 10, 8, 19]
Explanation: The postorder traversal of the given binary tree is [11, 13, 10, 8, 19].
Example 2
Input: root = [11, 15, N, 7]
Output: [7, 15, 11]
Explanation: The postorder traversal of the given binary tree is [7, 15, 11].
π Constraints
$1 \le \text{number of nodes} \le 3 \times 10^4$
$0 \le \text{node->data} \le 10^5$
β
My Approach
The optimal approach uses Morris Traversal technique to achieve postorder traversal with constant space complexity:
Morris Traversal for Postorder
Initialize Variables:
Use a current pointer starting at root.
Maintain a result array to store the traversal.
Traverse Using Right-Left Mirror:
For postorder, we traverse in reverse (root-right-left) and then reverse the result.
If current node has no right child, add it to result and move to left child.
Create Threading:
If right child exists, find the inorder predecessor (leftmost node in right subtree).
If predecessor's left is null, create thread by linking it to current node.
Add current node to result and move to right child.
Remove Threading:
If predecessor's left already points to current, remove the thread.
Move current to left child.
Reverse Result:
After traversal completes, reverse the result array to get correct postorder sequence.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n), where n is the number of nodes in the binary tree. Each node is visited at most twice - once for creating thread and once for removing it, making it linear time complexity.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for pointers. The result array is not counted as auxiliary space since it's required for output. No recursion stack or additional data structures are used.
βοΈ Code (C)
#define MAX_SIZE 100000
int* postOrder(struct Node* root, int* size) {
int* res = (int*)malloc(MAX_SIZE * sizeof(int));
*size = 0;
struct Node* curr = root;
while (curr) {
if (!curr->right) {
res[(*size)++] = curr->data;
curr = curr->left;
} else {
struct Node* pred = curr->right;
while (pred->left && pred->left != curr) pred = pred->left;
if (!pred->left) {
res[(*size)++] = curr->data;
pred->left = curr;
curr = curr->right;
} else {
pred->left = NULL;
curr = curr->left;
}
}
}
for (int i = 0, j = *size - 1; i < j; i++, j--) {
int temp = res[i];
res[i] = res[j];
res[j] = temp;
}
return res;
}
π§βπ» Code (C++)
class Solution {
public:
vector<int> postOrder(Node *root) {
vector<int> res;
Node *curr = root;
while (curr) {
if (!curr->right) {
res.push_back(curr->data);
curr = curr->left;
} else {
Node *pred = curr->right;
while (pred->left && pred->left != curr) pred = pred->left;
if (!pred->left) {
res.push_back(curr->data);
pred->left = curr;
curr = curr->right;
} else {
pred->left = NULL;
curr = curr->left;
}
}
}
reverse(res.begin(), res.end());
return res;
}
};
β Code (Java)
class Solution {
public ArrayList<Integer> postOrder(Node root) {
ArrayList<Integer> res = new ArrayList<>();
Node curr = root;
while (curr != null) {
if (curr.right == null) {
res.add(curr.data);
curr = curr.left;
} else {
Node pred = curr.right;
while (pred.left != null && pred.left != curr) pred = pred.left;
if (pred.left == null) {
res.add(curr.data);
pred.left = curr;
curr = curr.right;
} else {
pred.left = null;
curr = curr.left;
}
}
}
Collections.reverse(res);
return res;
}
}
π Code (Python)
class Solution:
def postOrder(self, root):
res = []
curr = root
while curr:
if not curr.right:
res.append(curr.data)
curr = curr.left
else:
pred = curr.right
while pred.left and pred.left != curr:
pred = pred.left
if not pred.left:
res.append(curr.data)
pred.left = curr
curr = curr.right
else:
pred.left = None
curr = curr.left
res.reverse()
return res
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated