lurenaa的博客

😂递归

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(!root)
return true;
return comp(root->left, root->right);
}
bool comp (TreeNode* l, TreeNode* r){
if(!l && !r) {
return true;
}
if(!l || !r)
return false;
bool mb = l->val == r->val;
bool lb = comp(l->left, r->right);
bool rb = comp(l->right, r->left);
return lb && rb && mb;
}
};

Accepted

195/195 cases passed (8 ms)

Your runtime beats 64.49 % of cpp submissions

Your memory usage beats 5.1 % of cpp submissions (15.3 MB)