给你一个二叉树的根节点 root , 检查它是否轴对称。
本文参考代码随想录的解法,增加了一些理解,如果有友友像我一样看完代码和视频还是不太明白的,可以参考一下~
源码来自 代码随想录
分析:
- 递归法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
// 1、 确定递归函数的参数和返回值
bool compare(TreeNode* left,TreeNode* right){
// 2. 确定终止条件
// 排除法,先判断存在节点为空的情况
if(left==NULL&&right!=NULL) return false;
else if(left!=NULL&&right==NULL) return false;
else if(left==NULL&&right==NULL) return true;
//排除节点不为空但不也不相等的情况
else if(left->val!=right->val) return false;
// 3. 确定单层递归的逻辑
// 在满足条件的时候继续往下走,判断内节点和外节点是否满足对称
bool outside=compare(left->left,right->right);
bool inside=compare(left->right,right->left);
bool isSame=outside&&inside;
return isSame;
//上面这段可以简写为:
// else return compare(left->left,right->right)&&compare(left->right,right->left);
}
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
return compare(root->left,root->right);
}
};
- 迭代法:使用队列来比较
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
// 创建队列,存放需要比较的节点
queue<TreeNode*> que;
que.push(root->left);
que.push(root->right);
while(!que.empty()){
TreeNode* leftNode=que.front();que.pop();
TreeNode* rightNode=que.front();que.pop();
if(!leftNode&&!rightNode) continue;
if((!leftNode||!rightNode||(leftNode->val!=rightNode->val))) return false;
//继续吧下一层的都推进来
que.push(leftNode->left);
que.push(rightNode->right);
que.push(leftNode->right);
que.push(rightNode->left);
}
return true;
}
};
- 栈的使用:
其实是把左右两个子树要比较的元素顺序放进一个容器,然后成对成对的取出来进行比较,
队列和栈都能实现
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if (root == NULL) return true;
stack<TreeNode*> st; // 这里改成了栈
st.push(root->left);
st.push(root->right);
while (!st.empty()) {
TreeNode* leftNode = st.top(); st.pop();
TreeNode* rightNode = st.top(); st.pop();
if (!leftNode && !rightNode) {
continue;
}
if ((!leftNode || !rightNode || (leftNode->val != rightNode->val))) {
return false;
}
st.push(leftNode->left);
st.push(rightNode->right);
st.push(leftNode->right);
st.push(rightNode->left);
}
return true;
}
};
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isSymmetric = function(root) {
if(root===null) return true;
let stack=[];
stack.push(root.left);
stack.push(root.right);
while(stack.length){
let rightNode=stack.pop();
let leftNode=stack.pop();
if(leftNode===null&&rightNode===null) continue;
if(leftNode===null||rightNode===null||leftNode.val!=rightNode.val) return false;
stack.push(leftNode.left);
stack.push(rightNode.right);
stack.push(leftNode.right);
stack.push(rightNode.left);
}
return true;
};