-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #559 from Rituraj-13/symmetric_tree
Solution of Symmetric tree problem on Leetcode
- Loading branch information
Showing
7 changed files
with
36 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-46.4 KB
CPP/LINKED LIST/SINGLY LINKED LIST/Maths For DSA/Sliding Window Problem/subarraysum.bin
Binary file not shown.
Binary file removed
BIN
-46.4 KB
CPP/LINKED LIST/SINGLY LINKED LIST/Maths For DSA/Sliding Window Problem/subarraysum.exe
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) ); | ||
|
||
} | ||
}; |