栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

对称二叉树

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

对称二叉树

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 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 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 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函数判断是否相同。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/889567.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号