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

第15天,坚持!

以下题目皆在Carl老师的 代码随想录 中(页面左边有对应题目的目录)有详细讲解,这是博主学习的心得和日志,感兴趣的建议去阅读一下原文。


102.二叉树的层序遍历

复习和巩固区域变量的基础

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        // 弄个队列
        queue<TreeNode*> que;
        // 把树的根节点推入队列
        if (root != nullptr) que.push(root);
        // 弄个二维数组,因为输出的结果中包含每层的数组
        vector<vector<int>> res;
        // 巩固基础知识:function/loop里创建的variable是local variable,随着function return销毁/重置
        // loop里的local variables随着每loop结束被销毁和重置
        while(!que.empty()){
            //当前层的长度, 每loop重置
            int size = que.size();
            //用来储存当前层的元素代表当层数组,每loop重置
            vector<int> vec;
            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);
            }
            //把本层的size--处理完填入vec放到二维数组中
            res.push_back(vec);

        }

        return res;
    }
};


226. 翻转二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if(root == nullptr) return root;
        swap(root->left, root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;   
    }
};


101. 对称二叉树

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
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 isSame = outside && inside;
        return isSame;

    }

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

    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值