Skip to content

Commit

Permalink
added solution for symmetric_tree in C++
Browse files Browse the repository at this point in the history
  • Loading branch information
Shubh2003 committed Oct 24, 2023
1 parent 75bee03 commit 5765d62
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions C++/symmetric_Tree.cpp
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);
}
};

0 comments on commit 5765d62

Please sign in to comment.