Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solution of Symmetric tree problem on Leetcode #559

Merged
merged 1 commit into from
Oct 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) );

}
};