JZ58 对称的二叉树
本文最后更新于:2022年4月9日 中午
Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
class Solution { public: bool isSymmetrical(TreeNode* pRoot) { if (pRoot == nullptr) return true; return isSym(pRoot->left, pRoot->right); } bool isSym(TreeNode* p1, TreeNode* p2) { if (p1 == nullptr && p2 == nullptr) return true; if (p1 == nullptr || p2 == nullptr) return false; if (p1->val != p2->val) return false; return isSym(p1->left, p2->right) && isSym(p1->right, p2->left); }
};
|