15代码随想录训练营day15|二叉树part02

1、层序遍历

 102. 二叉树的层序遍历 - 力扣(LeetCode)

class Solution {
public:
    vector> levelOrder(TreeNode* root) {
        queue que;
        vector ceil;
        vector> result;
        if (root == NULL) return result;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                TreeNode *temp = que.front();
                que.pop();
                ceil.push_back(temp->val);
                if (temp->left) que.push(temp->left);
                if (temp->right) que.push(temp->right);
            }
            result.push_back(ceil);
            ceil.clear();
        }
        return result;
    }
};

107. 二叉树的层序遍历 II - 力扣(LeetCode)

class Solution {
public:
    vector> levelOrderBottom(TreeNode* root) {
        queue que;
        vector> result;
        vector lev;
        if (!root) return result;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                TreeNode *temp = que.front();
                que.pop();
                lev.push_back(temp->val);
                if (temp->left) que.push(temp->left);
                if (temp->right) que.push(temp->right);
            }
            result.push_back(lev);
            lev.clear();
        }
        reverse(result.begin(), result.end());
        return result;
    }
};

199. 二叉树的右视图 - 力扣(LeetCode)

class Solution {
public:
    vector rightSideView(TreeNode* root) {
        //vector> tree;
        vector lev;
        vector result;
        queue que;
        if (!root) return result;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                TreeNode *temp = que.front();
                lev.push_back(temp->val);
                que.pop();
                if (temp->right) que.push(temp->right);
                if (temp->left) que.push(temp->left);
            }
            result.push_back(lev.front());
            lev.clear();
        }
        return result;
    }
};

637. 二叉树的层平均值 - 力扣(LeetCode)

class Solution {
public:
    vector averageOfLevels(TreeNode* root) {
        vector result;
        vector lev;
        queue que;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                TreeNode *temp = que.front();
                que.pop();
                lev.push_back(temp->val);
                if (temp->left) que.push(temp->left);
                if (temp->right) que.push(temp->right);
            }
            double sum = 0;
            for (int i = 0; i < lev.size(); i++) {
                sum += lev[i];
            }
            result.push_back(sum/(double)lev.size());
            lev.clear();
        }
        return result;
    }
};

 429. N 叉树的层序遍历 - 力扣(LeetCode)

class Solution {
public:
    vector> levelOrder(Node* root) {
        vector> result;
        vector lev;
        queue que;
        if (!root) return result;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                Node *temp = que.front();
                que.pop();
                lev.push_back(temp->val);
                for (int i = 0; i < temp->children.size(); i++) {
                    que.push(temp->children[i]);
                }
            }
            result.push_back(lev);
            lev.clear();
        }
        return result;
    }
};

515. 在每个树行中找最大值 - 力扣(LeetCode)

class Solution {
public:
    vector largestValues(TreeNode* root) {
        vector result;
        queue que;
        stack max;
        if (!root) return result;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode *temp = que.front();
                que.pop();
                if (!max.empty() && max.top() < temp->val) {
                    max.pop();
                }
                if (max.empty()) max.push(temp->val);
                if (temp->left) que.push(temp->left);
                if (temp->right) que.push(temp->right);
            }
            result.push_back(max.top());
            max.pop();
        }
        return result;
    }
};

116. 填充每个节点的下一个右侧节点指针 - 力扣(LeetCode)

class Solution {
public:
    Node* connect(Node* root) {
        queue que;
        if (!root) return root;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                Node *temp = que.front();
                que.pop();
                if (temp->left) que.push(temp->left);
                if (temp->right) que.push(temp->right);
                if (i != size - 1) {
                    temp->next = que.front();
                } else {
                    temp->next = NULL;
                }
            }
        }
        return root;
    }
};

2、翻转二叉树

226. 翻转二叉树 - 力扣(LeetCode)

 2.1、递归法

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (!root) return root;
        swap(root->left, root->right);
        invertTree(root->left);
        invertTree(root->right);
        return root;
    }
};

 2.2、迭代法

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        if (!root) return root;
        stack st;
        st.push(root);
        while (!st.empty()) {
            TreeNode *temp = st.top();
            st.pop();
            swap(temp->left, temp->right);
            if (temp->right) st.push(temp->right);
            if (temp->left) st.push(temp->left);
        }
        return root;
    }
};

2.3、层序遍历

class Solution {
public:
    TreeNode* invertTree(TreeNode* root) {
        queue que;
        if (!root) return root;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            for (int i = 0; i < size; i++) {
                TreeNode *temp = que.front();
                que.pop();
                swap(temp->left, temp->right);
                if (temp->left) que.push(temp->left);
                if (temp->right) que.push(temp->right);
            }
        }
        return root;
    }
};

3、对称二叉树

101. 对称二叉树 - 力扣(LeetCode)

class Solution {
public:
    bool compare(TreeNode* left, TreeNode* right) {
        if (!left && right) return false;
        else if (left && !right) return false;
        else if (!left && !right) return true;
        else if (left->val != right->val) return false;
        return compare(left->left, right->right) && compare(left->right, right->left);
    }

    bool isSymmetric(TreeNode* root) {
        return compare(root->left, root->right);
    }
};

你可能感兴趣的:(代码随想录训练营,算法,leetcode,c++)