8000 Refactored isSymmetric · BenjaminUJun/leetcode_Java@bceab98 · GitHub
[go: up one dir, main page]

Skip to content

Commit bceab98

Browse files
committed
Refactored isSymmetric
1 parent 8f34704 commit bceab98

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

C++/chapTree.tex

+8-7
Original file line numberDiff line numberDiff line change
@@ -833,14 +833,15 @@ \subsubsection{递归版}
833833
class Solution {
834834
public:
835835
bool isSymmetric(TreeNode *root) {
836-
return root ? isSymmetric(root->left, root->right) : true;
836+
if (root == nullptr) return true;
837+
return isSymmetric(root->left, root->right);
837838
}
838-
bool isSymmetric(TreeNode *left, TreeNode *right) {
839-
if (!left && !right) return true; // 终止条件
840-
if (!left || !right) return false; // 终止条件
841-
return left->val == right->val // 三方合并
842-
&& isSymmetric(left->left, right->right)
843-
&& isSymmetric(left->right, right->left);
839+
bool isSymmetric(TreeNode *p, TreeNode *q) {
840+
if (p == nullptr && q == nullptr) return true; // 终止条件
841+
if (p == nullptr || q == nullptr) return false; // 终止条件
842+
return p->val == q->val // 三方合并
843+
&& isSymmetric(p->left, q->right)
844+
&& isSymmetric(p->right, q->left);
844845
}
845846
};
846847
\end{Code}

C++/leetcode-cpp.pdf

1 Byte
Binary file not shown.

0 commit comments

Comments
 (0)
0