forked from panditakshay402/Hacktoberfest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
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 panditakshay402#218 from Shubh2003/symmetric_tree
added solution for symmetric_tree in C++
- Loading branch information
Showing
1 changed file
with
22 additions
and
0 deletions.
There are no files selected for viewing
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,22 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* 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 solve(TreeNode* left, TreeNode* right){ | ||
if(!left && !right) return true; | ||
if(!left || !right) return false; | ||
return (left->val == right->val && solve(left->left, right->right) && solve(left->right, right->left)); | ||
} | ||
bool isSymmetric(TreeNode* root) { | ||
return solve(root->left, root->right); | ||
} | ||
}; |