jayati

Symmetric Tree

Mar 12th, 2023
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.94 KB | None | 0 0
  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.  
  13. class Solution {
  14. public:
  15.     bool helper(struct TreeNode* node1, struct TreeNode* node2) {
  16.     if (!node1 && !node2)
  17.         return 1;
  18.     if (!node1 && node2 || node1 && !node2)
  19.         return 0;
  20.     if (node1->val != node2->val)
  21.         return 0;
  22.     bool call;
  23.     call = helper(node1->left, node2->right);
  24.     if (!call)
  25.         return 0;
  26.     call = helper(node1->right, node2->left);
  27.     return call;
  28. }
  29.  
  30.     bool isSymmetric(TreeNode* root) {
  31.         if(!root)
  32.         {
  33.             return 1;
  34.         }
  35.         return helper(root->left,root->right);
  36.        
  37.     }
  38. };
Advertisement
Add Comment
Please, Sign In to add comment