代码随想录刷题打卡day14

102. 二叉树的层序遍历模板,记下来,也理解了。

代码随想录刷题打卡day14_第1张图片

/**
 * 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> levelOrder(TreeNode* root) {
        //队列que保持了树节点的顺序,使得你可以将元素添加到队列的尾部,并从队列的头部取出元素
        queue que;
        if(root!=NULL) que.push(root);
        //用于存储二叉树的层序遍历结果
        vector> result;
        while(!que.empty()){
            //记录的每层的节点个数
            //这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化
            int size = que.size();
            //只是用来存队列中头节点的值,最后存在大vector中
            vector vec;
            for(int i=0;ival);
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
            }
            result.push_back(vec);
        }
    return result;
    }
};

226. 翻转二叉树

1.基于递归法

/**
 * 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==NULL) return root;
        swap(root->left,root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

2.基于迭代法

/**
 * 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==NULL) return root;
        stack 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;
    }
};

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!=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);
    }
};

你可能感兴趣的:(代码随想录刷题打卡记录,算法,数据结构,leetcode)