Skip to content

Commit bc6b9bb

Browse files
authored
Create 101. Symmetric Tree
1 parent 3719337 commit bc6b9bb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

101. Symmetric Tree

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
bool check(TreeNode* lSub, TreeNode* rSub)
15+
{
16+
//base cases
17+
if(lSub == NULL && rSub == NULL)
18+
return true;
19+
20+
if(lSub == NULL || rSub == NULL)
21+
return false;
22+
23+
if(lSub -> val != rSub -> val)
24+
return false;
25+
26+
27+
bool leftAns = check(lSub -> left, rSub -> right);
28+
bool rightAns = check(lSub -> right, rSub -> left);
29+
30+
return leftAns && rightAns;
31+
}
32+
33+
bool isSymmetric(TreeNode* root) {
34+
return check(root -> left , root -> right);
35+
}
36+
};

0 commit comments

Comments
 (0)