Skip to content

Commit 257dfc5

Browse files
Merge pull request #63 from RohitY24/patch#2
commit tree_foldable.cpp
2 parents a957c0a + cec6743 commit 257dfc5

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Trees/tree_foldable.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// check if a Tree is foldable or not
2+
3+
// approach:
4+
// If one of the two trees is empty, return false; if both are empty, return true
5+
// If one of the subtree is found out to be mirror of the other one, return true
6+
7+
bool find(node *node1, node *node2)
8+
{
9+
if (node1 == NULL && node2 == NULL)
10+
{
11+
return true;
12+
}
13+
if (node1 == NULL || node2 == NULL)
14+
{
15+
return false;
16+
}
17+
return find(node1->left, node2->right) && find(node1->right, node2->left);
18+
}
19+
20+
bool foldableCheck(node *root)
21+
{
22+
if (root == NULL)
23+
{
24+
return true;
25+
}
26+
27+
return find(root->left, root->right);
28+
}

0 commit comments

Comments
 (0)