Skip to content

Commit c95d2e7

Browse files
committed
add iterative appraoch
1 parent 3345271 commit c95d2e7

File tree

1 file changed

+51
-2
lines changed

1 file changed

+51
-2
lines changed

Trees/Binary Trees/is_symmetric.c++

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
- Therefore, the space complexity is O(N) due to the recursion stack usage.
3232
3333
It's important to note that the space complexity can be optimized by using an iterative approach instead of recursion. By using an iterative algorithm that leverages a stack or queue to perform a level-order traversal, we can achieve a space complexity of O(W), where W is the maximum width (number of nodes at the same level) of the binary tree.
34-
34+
3535
*/
3636
#include <iostream>
3737

@@ -46,7 +46,7 @@ public:
4646
};
4747

4848
// SymmetricalTree checks if a binary tree is symmetrical.
49-
bool SymmetricalTree(BinaryTree* tree) {
49+
bool SymmetricalTreeRecursive(BinaryTree* tree) {
5050
// Call the helper function to check if the left and right subtrees are mirrored.
5151
return treesAreMirrored(tree->Left, tree->Right);
5252
}
@@ -64,6 +64,55 @@ bool treesAreMirrored(BinaryTree* left, BinaryTree* right) {
6464
return left == right;
6565
}
6666

67+
// Approach 2: Iterative Approach using Stack
68+
/*
69+
In this iterative approach, we use two stacks (stackLeft and stackRight) to perform a mirror traversal of the
70+
left and right subtrees. The process is similar to the original code snippet but implemented iteratively
71+
using a while loop and stacks. The stacks are initialized with the left and right children of the root node,
72+
and in each iteration, we compare the corresponding nodes from both stacks and check for asymmetry.
73+
The children of the left and right nodes are pushed onto their respective stacks in reverse order to
74+
maintain the mirror traversal. The loop continues until both stacks are empty or an asymmetry is detected.
75+
Finally, the function returns whether the tree is symmetric or not.
76+
77+
*/
78+
struct BinaryTree {
79+
int value;
80+
BinaryTree* left;
81+
BinaryTree* right;
82+
};
83+
84+
bool SymmetricalTreeIterative(BinaryTree* tree) {
85+
std::stack<BinaryTree*> stackLeft;
86+
std::stack<BinaryTree*> stackRight;
87+
stackLeft.push(tree->left); // Initialize stackLeft with the left child of the root node
88+
stackRight.push(tree->right); // Initialize stackRight with the right child of the root node
89+
90+
// Perform mirror traversal of the left and right subtrees
91+
while (!stackLeft.empty()) {
92+
BinaryTree* left = stackLeft.top();
93+
BinaryTree* right = stackRight.top();
94+
stackLeft.pop();
95+
stackRight.pop();
96+
97+
if (left == nullptr && right == nullptr) {
98+
continue; // Both left and right subtrees are symmetric, continue to the next iteration
99+
}
100+
101+
if (left == nullptr || right == nullptr || left->value != right->value) {
102+
return false; // Asymmetry detected, tree is not symmetric
103+
}
104+
105+
// Push the children of left and right onto the respective stacks in reverse order
106+
stackLeft.push(left->left);
107+
stackLeft.push(left->right);
108+
stackRight.push(right->right);
109+
stackRight.push(right->left);
110+
}
111+
112+
return true; // Tree is symmetric
113+
}
114+
115+
67116
int main() {
68117
// Create a binary tree for testing
69118
BinaryTree* tree = new BinaryTree();

0 commit comments

Comments
 (0)