day15打卡

day15打卡

226. 翻转二叉树

递归解法:

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        //出口
        if(root == nullptr) return root;
        swap(root->left, root->right);
        TreeNode* left = invertTree(root->left);
        TreeNode* right = invertTree(root->right);
        root->left = left;
        root->right = right;
        return root;
    }
};

迭代解法:

  • 方法一

掌握迭代法遍历二叉树即可

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == nullptr) return root;
        stack<TreeNode*> st;
        st.push(root);
        while(!st.empty())
        {
            TreeNode* cur = st.top();
            st.pop();
            swap(cur->left, cur->right);
            if(cur->right != nullptr) st.push(cur->right);//左节点交换到了右边
            if(cur->left != nullptr) st.push(cur->left);//反转顺序无所谓
        }
        return root;
    }
};
  • 方法二

层序遍历

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == nullptr) return root;
        queue<TreeNode*> q;
        q.push(root);
        while(!q.empty())
        {
            for(int i = 0; i < q.size(); i++)//遍历完本层节点后才会进入下一层节点
            {
                TreeNode* top = q.front();
                q.pop();
                swap(top->left, top->right);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
        }
        return root;
    }
};

101. 对称二叉树

递归解法:

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    bool dfs(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 Left = dfs(left->left, right->right);
        bool Right = dfs(left->right, right->left); 
        return Left && Right;
    }
    bool isSymmetric(TreeNode* root) {
        return dfs(root->left, root->right);
    }
};

迭代解法:

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        stack<TreeNode*> st;
        st.push(root->left);
        st.push(root->right);
        while(!st.empty())
        {
            TreeNode* right = st.top();
            st.pop();
            TreeNode* left = 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;
    }
};
  • 队列
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        queue<TreeNode*> q;
        q.push(root->left);
        q.push(root->right);
        while(!q.empty())
        {
            TreeNode* left = q.front();
            q.pop();
            TreeNode* right = q.front();
            q.pop();
            //如果都为空则为true,跳过此次循环
            if(!left && !right) continue;
            //如果一个为空或者节点中数字不同,就直接返回false
            if(!left || !right || (left->val != right->val)) return false;
            //进入下一层节点判断
            q.push(left->left);
            q.push(right->right);
            q.push(left->right);
            q.push(right->left);

        }
        //遍历到最后返回true
        return true;
    }
};
  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值