题解 | #对称的二叉树#
对称的二叉树
https://www.nowcoder.com/practice/ff05d44dfdb04e1d83bdbdab320efbcb
/**
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* };
*/
#include <utility>
class Solution {
public:
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param pRoot TreeNode类
* @return bool布尔型
*/
bool isSymmetricalFun(pair<TreeNode*, TreeNode*>
com) { //输入两个树的根节点,判断两个树是否对称
if (com.first == nullptr && com.second == nullptr) {
return true;
} else if (com.first == nullptr || com.second == nullptr) {
return false;
} else {
return (com.first->val == com.second->val) &&
isSymmetricalFun(make_pair(com.first->left, com.second->right)) &&
isSymmetricalFun(make_pair(com.first->right, com.second->left));
}
}
bool isSymmetrical(TreeNode* pRoot) {
// write code here
if(pRoot==nullptr){
return true;
}
return isSymmetricalFun(make_pair(pRoot->left, pRoot->right));
}
};

