代码随想录算法训练营第15天

层序遍历

思路:

注意:

代码:

cpp 复制代码
/**
 * 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;
        vector<vector<int>>res;
        if(root){ que.push(root);}
        while(!que.empty()){
            int size = que.size();
            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);
            }

            res.push_back(vec);

        }
        return res;
    }
};

226.翻转二叉树 (优先掌握递归)

思路:

复制代码
	确定  递归函数的参数 和  返回值  
	确定  循环终止条件                    if(root == nullptr) return root;
	确定单层递归遍历逻辑 (中, 左  右等等)
				        invertTree(root->left);
    				invertTree(root->right);
    				swap(root->left, root->right);

注意:左右中

代码:

cpp 复制代码
class Solution {
public:


    TreeNode* invertTree(TreeNode* root) {
        if(root == nullptr) return root;
        invertTree(root->left);
        invertTree(root->right);
        swap(root->left, root->right);
        return root;
    }
};

101. 对称二叉树 (优先掌握递归)

思路:

复制代码
	传入参数:
			left,  right  
	终止条件:
		判断 左为空  右不为空  false
		判断 左为非空  右为空  false
		判断 左为空  右为空  true
	单层递归逻辑:
		compair(left->left, right->right) && compair(left->right, right->left);

注意: 不能利用left->val == right->val 来判断 两棵树是 对称二叉树

代码:

cpp 复制代码
class Solution {
public:
bool compair(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;
    // else if(left->val == right->val) return true;
    
    return compair(left->left, right->right) && compair(left->right, right->left);
     
    


}
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        return compair(root->left, root->right);
    }
};
相关推荐
起个名字费劲死了24 分钟前
QT + Socket 客户端/服务端 公网通讯
服务器·c++·qt·socket
我是一只小青蛙88839 分钟前
位图与布隆过滤器:高效数据结构解析
开发语言·c++·算法
xiaoye-duck1 小时前
吃透C++类和对象(下):初始化列表深度解析
c++
曼巴UE51 小时前
UE5 C++ GameInstanceSubsystem 在学习
c++·ue5·ue
Ethan Wilson1 小时前
VS2019 C++20 模块相关 C1001: 内部编译器错误
开发语言·c++·c++20
m0_748252382 小时前
Bootstrap 5 加载效果实现方法
c++
人工智能AI技术3 小时前
GitHub Copilot 2026新功能实操:C++跨文件上下文感知开发,效率翻倍技巧
c++·人工智能
大志若愚YYZ3 小时前
ROS2学习 C++中的this指针
c++·学习·算法
玖釉-4 小时前
[Vulkan 学习之路] 16 - 最终章:渲染循环与同步 (Rendering & Presentation)
c++·windows·图形渲染
狗狗学不会4 小时前
Pybind11 封装 RK3588 全流程服务:Python 写逻辑,C++ 跑并发,性能起飞!
c++·人工智能·python·目标检测