Skip to content

Commit

Permalink
Merge pull request #559 from Rituraj-13/symmetric_tree
Browse files Browse the repository at this point in the history
Solution of Symmetric tree problem on Leetcode
  • Loading branch information
gantavyamalviya committed Oct 5, 2022
2 parents d349592 + e4ce10a commit 15aa990
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 0 deletions.
Binary file removed CPP/LINKED LIST/CIRCULAR LINKED LIST/test4.bin
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions Leetcode-solutions/Symmetric_Tree/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Leetcode question #101

[Click here to view the question on Leetcode.](https://leetcode.com/problems/symmetric-tree/)
33 changes: 33 additions & 0 deletions Leetcode-solutions/Symmetric_Tree/Symmetric_Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
bool isSymmetric(TreeNode* root) {

if(!root)
return true;

return areMirror(root->left, root->right);

}

bool areMirror(TreeNode* t1, TreeNode* t2){
if(!t1 || !t2)
return (t1 == t2);

if(t1->val != t2->val)
return false;

return ( areMirror(t1->left, t2->right) && areMirror(t1->right, t2->left) );

}
};

0 comments on commit 15aa990

Please sign in to comment.