Check if a given binary tree is symmetric.

class Solution { public: bool isSymmetricHelper(TreeNode * left, TreeNode * right){ if (left==nullptr && right == nullptr){ return true; } else if (left == nullptr && right != nullptr){ return false; } else if (left != nullptr && right == nullptr){ return false; } else if (left->value == right->value && isSymmetricHelper(left->left, right->right) && isSymmetricHelper(left->right, right->left)) { return true; } return false; } bool isSymmetric(TreeNode* root) { // write your solution here if (root == nullptr){ return true; } return isSymmetricHelper(root->left, root->right); } };
Create a helper function.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.