Skip to content

Commit 4a8152f

Browse files
committed
add question and explanation
1 parent 7a863d8 commit 4a8152f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Trees/Binary Trees/is_symmetric.c++

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
Explanation:
6+
7+
1. The code defines a class `BinaryTree` representing a binary tree node. It has an `int` value and pointers
8+
to its left and right children.
9+
2. The `SymmetricalTree` function is the main entry point. It calls the helper function `treesAreMirrored`
10+
to check if the left and right subtrees are mirrored.
11+
3. The `treesAreMirrored` function checks if two binary trees are mirrored. It uses recursion to compare
12+
corresponding nodes in the left and right subtrees.
13+
4. In the `treesAreMirrored` function, the base case checks if both the left and right trees are non-null
14+
and have the same value. If so, it recursively checks if their subtrees are mirrored.
15+
5. If either the left or right tree is null or their values are not equal, they are not mirrored.
16+
If both the left and right trees are null, they are considered mirrored.
17+
6. In the `main` function, a binary tree is created for testing purposes.
18+
7. The `SymmetricalTree` function is called to check if the binary tree is symmetrical.
19+
8. The result is printed to the console.
20+
9. Memory cleanup is performed by deleting the dynamically allocated nodes.
21+
22+
Note: This code snippet assumes the use of a C++ compiler and standard library.
23+
*/
124
#include <iostream>
225

326
using namespace std;

0 commit comments

Comments
 (0)