代码随想录算法训练营第15天| 二叉树层序遍历、226.翻转二叉树、 101. 对称二叉树*

二叉树层序遍历

代码

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        if (root == nullptr) return {}; // 添加对空树的处理
​
        queue<TreeNode*> que;
        que.push(root);
        vector<vector<int>> result;
​
        while(!que.empty()) { // 修改了这里的语法错误,添加了缺失的闭合括号
            int levelSize = que.size(); // 获取当前层的节点数量
            vector<int> temp;
​
            for(int i = 0; i < levelSize; ++i) {
                TreeNode* node = que.front(); // 使用front而不是top
                que.pop();
                temp.push_back(node->val);
​
                if(node->left != nullptr) que.push(node->left); // 修改了这里的条件判断逻辑
                if(node->right != nullptr) que.push(node->right); // 修改了这里的条件判断逻辑
            }
​
            if(!temp.empty()) result.push_back(temp);
        }
​
        return result;
    }
};
示例代码:迭代法
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        vector<vector<int>> result;
        while (!que.empty()) {
            int size = que.size();
            vector<int> vec;
            // 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                vec.push_back(node->val);
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
            result.push_back(vec);
        }
        return result;
    }
};
示例代码:递归法
# 递归法
class Solution {
public:
    void order(TreeNode* cur, vector<vector<int>>& result, int depth)
    {
        if (cur == nullptr) return;
        if (result.size() == depth) result.push_back(vector<int>());
        result[depth].push_back(cur->val);
        order(cur->left, result, depth + 1);
        order(cur->right, result, depth + 1);
    }
    vector<vector<int>> levelOrder(TreeNode* root) {
        vector<vector<int>> result;
        int depth = 0;
        order(root, result, depth);
        return result;
    }
};

思路

迭代法使用队列进行辅助

226.翻转二叉树

题目

题目链接

代码

class Solution {
public:
    void vertrval(TreeNode* cur){
        if(cur == nullptr) return;
        TreeNode* temp = cur -> left;
        cur -> left = cur ->right;
        cur->right = temp;
        vertrval(cur->left);
        vertrval(cur->right);
    }
    TreeNode* invertTree(TreeNode* root) {
        vertrval(root);
        return root;
    }
};
示例代码
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (root == NULL) return root;
        swap(root->left, root->right);  // 中
        invertTree(root->left);         // 左
        invertTree(root->right);        // 右
        return root;
    }
};

思路

递归法较为简单,迭代法暂不掌握

## 101. 对称二叉树*

题目

题目链接

代码

示例代码
class Solution {
public:
    bool compare(TreeNode* left, TreeNode* right) {
        // 首先排除空节点的情况
        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;
​
        // 此时就是:左右节点都不为空,且数值相同的情况
        // 此时才做递归,做下一层的判断
        bool outside = compare(left->left, right->right);   // 左子树:左、 右子树:右
        bool inside = compare(left->right, right->left);    // 左子树:右、 右子树:左
        bool isSame = outside && inside;                    // 左子树:中、 右子树:中 (逻辑处理)
        return isSame;
​
    }
    bool isSymmetric(TreeNode* root) {
        if (root == NULL) return true;
        return compare(root->left, root->right);
    }
};

思路

非常考验递归的思路是否清楚

  • 16
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值