From 3d850f8a3fe94081707f50de27f00078090edfe0 Mon Sep 17 00:00:00 2001 From: Stackingrule <38368554+Stackingrule@users.noreply.github.com> Date: Thu, 17 Dec 2020 14:57:39 +0800 Subject: [PATCH] feat:add Solution.cpp for 0101.Symmetric Tree --- .../0100-0199/0101.Symmetric Tree/Solution.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 solution/0100-0199/0101.Symmetric Tree/Solution.cpp diff --git a/solution/0100-0199/0101.Symmetric Tree/Solution.cpp b/solution/0100-0199/0101.Symmetric Tree/Solution.cpp new file mode 100644 index 0000000000000..746ded3c99023 --- /dev/null +++ b/solution/0100-0199/0101.Symmetric Tree/Solution.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + bool isSymmetric(TreeNode* root) { + if (!root) return true; + return isSymmetric(root->left, root->right); + } + +private: + bool isSymmetric(TreeNode* left, TreeNode* right) { + if (!left && !right) return true; + if (!left && right || left && !right || left->val != right->val) return false; + return isSymmetric(left->left, right->right) && isSymmetric(left->right, right->left); + } +}; \ No newline at end of file