Skip to content

Commit a93b3f2

Browse files
committed
add issymmetric in js and py
1 parent 160f613 commit a93b3f2

File tree

2 files changed

+86
-0
lines changed

2 files changed

+86
-0
lines changed

Trees/Binary Trees/is_symmetric.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
Write a function that takes in a Binary Tree and returns if that tree is symmetrical. A tree is symmetrical
3+
if the left and right subtrees are mirror images of each other.
4+
*/
5+
class BinaryTree {
6+
constructor(value, left = null, right = null) {
7+
this.value = value;
8+
this.left = left;
9+
this.right = right;
10+
}
11+
}
12+
13+
function isSymmetricalTreeIterative(tree) {
14+
const stackLeft = (tree && [tree.left]) || []; // Initialize stackLeft with the left child of the root node
15+
const stackRight = (tree && [tree.right]) || []; // Initialize stackRight with the right child of the root node
16+
17+
// Perform mirror traversal of the left and right subtrees
18+
while (stackLeft.length > 0) {
19+
const left = stackLeft.pop() || null; // Pop the top node from stackLeft
20+
const right = stackRight.pop() || null; // Pop the top node from stackRight
21+
22+
if (!left && !right) {
23+
continue; // Both left and right subtrees are symmetric, continue to the next iteration
24+
}
25+
26+
if (!left || !right || left.value !== right.value) {
27+
return false; // Asymmetry detected, tree is not symmetric
28+
}
29+
30+
// Push the children of left and right onto the respective stacks in reverse order
31+
if (left) {
32+
stackLeft.push(left.left, left.right);
33+
}
34+
if (right) {
35+
stackRight.push(right.right, right.left);
36+
}
37+
}
38+
39+
return true; // Tree is symmetric
40+
}
41+
42+
// Example usage:
43+
// Construct a symmetric tree
44+
const symmetricTree = new BinaryTree(
45+
1,
46+
new BinaryTree(2, new BinaryTree(3), new BinaryTree(4)),
47+
new BinaryTree(2, new BinaryTree(4), new BinaryTree(3))
48+
);
49+
50+
console.log(isSymmetricalTreeIterative(symmetricTree)); // Output: true

Trees/Binary Trees/is_symmetric.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
'''
2+
Write a function that takes in a Binary Tree and returns if that tree is symmetrical. A tree is symmetrical
3+
if the left and right subtrees are mirror images of each other.
4+
'''
5+
class BinaryTree:
6+
def __init__(self, value, left=None, right=None):
7+
self.value = value
8+
self.left = left
9+
self.right = right
10+
11+
def is_symmetrical_tree_iterative(tree):
12+
stack_left = [tree.left] if tree else [] # Initialize stackLeft with the left child of the root node
13+
stack_right = [tree.right] if tree else [] # Initialize stackRight with the right child of the root node
14+
15+
# Perform mirror traversal of the left and right subtrees
16+
while stack_left:
17+
left = stack_left.pop() if stack_left else None
18+
right = stack_right.pop() if stack_right else None
19+
20+
if not left and not right:
21+
continue # Both left and right subtrees are symmetric, continue to the next iteration
22+
23+
if not left or not right or left.value != right.value:
24+
return False # Asymmetry detected, tree is not symmetric
25+
26+
# Push the children of left and right onto the respective stacks in reverse order
27+
stack_left.extend([left.left, left.right] if left else [])
28+
stack_right.extend([right.right, right.left] if right else [])
29+
30+
return True # Tree is symmetric
31+
32+
# Example usage:
33+
# Construct a symmetric tree
34+
symmetric_tree = BinaryTree(1, BinaryTree(2, BinaryTree(3), BinaryTree(4)), BinaryTree(2, BinaryTree(4), BinaryTree(3)))
35+
36+
print(is_symmetrical_tree_iterative(symmetric_tree)) # Output: True

0 commit comments

Comments
 (0)