对称二叉树

class Solution {
public:
    bool compare(TreeNode* left, TreeNode* right) {
        if (left == nullptr && right == nullptr) {
            return false;
        } else if (left != nullptr && right == nullptr) {
            return false;
        } else if (left == nullptr && right == nullptr) {
            return true;
        } else if (left->val != right->val) {
            return false;
        }
        bool outside = compare(left->left, right->right);
        bool inside = compare(left->right, right->left);
        bool res = inside && outside;
        return res;
    }

    bool isSymmetric(TreeNode* root) {
        if (root == nullptr) {
            return true;
        }
        return compare(root->left, root->right);
    }
};

方法:递归法 

思路:要比较树的内侧结点和外侧结点,所以对于一个结点,遍历的顺序是左子树是左右中,右子树是右左中。

class Solution {
public:
    bool isSameTree(TreeNode* p, TreeNode* q) {
        stack<TreeNode*> st;
        st.push(p);
        st.push(q);
        while (!st.empty()) {
            TreeNode* q1 = st.top();
            st.pop();
            TreeNode* p1 = st.top();
            st.pop();
            if (q1 == nullptr && p1 == nullptr) {
                continue;
            } 
            if (!q1 || !p1 || (q1->val != p1->val)) {
                return false;
            }
            st.push(q1->left);
            st.push(p1->left);
            st.push(q1->right);
            st.push(p1->right);
        }
        return true;
    }
};

 方法:用栈或队列使用迭代法

大致思路就是一对一对的比较。

 

class Solution {
public:
    bool judge (TreeNode* p, TreeNode* q) {
        stack<TreeNode*> st;
        st.push(p);
        st.push(q);
        while (!st.empty()) {
            TreeNode* q1 = st.top();
            st.pop();
            TreeNode* p1 = st.top();
            st.pop();
            if (p1 == nullptr && q1 == nullptr) {
                continue;
            } 
            if (!q1 || ! p1 || (p1->val != q1->val)) {
                return false;
            }
            st.push(q1->left);
            st.push(p1->left);
            st.push(q1->right);
            st.push(p1->right);
        }
        return true;
    }

    bool isSubtree(TreeNode* root, TreeNode* subRoot) {
        queue<TreeNode*> que;
        if (subRoot == nullptr) {
            return true;
        }
        if (root == nullptr && subRoot != nullptr) {
            return false;
        }
        if (root != nullptr) {
            que.push(root);
        }
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (judge(node, subRoot)) {
                    return true;
                }
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right); 
            }
        }
        return false;
    }
};

思路:层序遍历的同时,每个结点去调用judge函数判断是否相同。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值