代码随想录 day15

226.翻转二叉树

递归

前序

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (nullptr == root) return root;

        // 中 左 右
        swap(root->left, root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

中序

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (nullptr == root) return root;

        // 左 中 右
        invertTree(root->left);
        swap(root->left, root-> right);
        
        // 注意此处:为何是 root->left ???
        // 因为在上方节点进行了交换,导致原来是左节点的位置变成了右节点
        // 所有此处使用 root->left 相当于之前的 root->right
        invertTree(root->left);
        return root;
    }
};

后序

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (nullptr == root) return root;

        invertTree(root->left);
        invertTree(root->right);
        swap(root->left, root->right);
        return root;
    }
};

迭代

前序

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (nullptr == root) return  root;
        stack<TreeNode*> st;
        st.push(root);

        while (!st.empty()) {
            // 中 左 右
            TreeNode *cur = st.top();
            st.pop();
            swap(cur->left, cur->right);
            if (cur->left) st.push(cur->left);
            if (cur->right)st.push(cur->right);
        }
        return root;
    }
};

统一迭代前序

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (nullptr == root) return root;
        stack<TreeNode*> st;
        st.push(root);

        while (!st.empty()) {
            TreeNode *cur = st.top();
            st.pop();
            if (nullptr != cur) {
                // 中 左 右
                if (cur->right)st.push(cur->right);
                if (cur->left) st.push(cur->left);
                st.push(cur);
                st.push(nullptr);
            } else {
                cur = st.top();
                st.pop();
                swap(cur->left, cur->right);
            }
        }
        return root;
    }
};

层序遍历

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (nullptr == root) return root;
        queue<TreeNode*> que;
        que.push(root);

        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode*cur = que.front();
                que.pop();
                swap(cur->left, cur->right);
                if (cur->left) que.push(cur->left);
                if (cur->right)que.push(cur->right);
            }
        }
        return root;
    }
};

101. 对称二叉树

递归

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

        // 注意节点需要对称
        bool outside = traversal(left->left, right->right);
        bool inside = traversal(left->right, right->left);
        return outside && inside;

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

使用队列迭代

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (nullptr == root) return true;
        queue<TreeNode*> que;
        que.push(root->left);
        que.push(root->right);

        while (!que.empty()) {
            TreeNode* left = que.front(); que.pop();
            TreeNode* right = que.front();que.pop();

            if (!left && !right) {
                continue;
            }
            if (!left || !right || (left->val != right->val)) {
                return false;
            }
            que.push(left->left);
            que.push(right->right);
            que.push(left->right);
            que.push(right->left);
        }
        return true;
    }
};

使用栈迭代

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if (nullptr == root) return true;
        stack<TreeNode*> st;
        st.push(root->left);
        st.push(root->right);

        while (!st.empty()) {
            TreeNode *left = st.top(); st.pop();
            TreeNode *right = st.top();st.pop();
            
            if (!left && !right) continue;
            if (!left || !right || (left->val != right->val)) return false;
            
            st.push(left->left);
            st.push(right->right);
            st.push(left->right);
            st.push(right->left);
        }
        return true;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值