代码随想录算法训练营day15 || 层序遍历、翻转二叉树,对称二叉树

层序遍历我主要记这3个题

首先是模板题

lc.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> levelOrder(TreeNode* root) {
        queue que;
        if (root != nullptr) que.push(root);
        vector> result;
        while (!que.empty()) {
            int size = que.size();
            vector vec;
            for (int i = 0; i < size; i++) {
                //动态遍历的时候size动态变化,会有下一层的进来
                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;
    }
};

lc.429:n叉树的层序遍历

和二叉树的层序遍历相比,向下遍历需要用一个for循环遍历当层然后再把值放入数组中

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector _children) {
        val = _val;
        children = _children;
    }
};
*/

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

lc.117: 填充每个节点的下一个右侧节点指针 II

设置两个节点node和preNode,preNode指向根节点,如果被遍历的节点本身是根节点,直接把preNode值赋予给node;不是根节点则让preNode的next指针指向node,然后向后面移动preNode的指针

/*
// Definition for a Node.
class Node {
public:
    int val;
    Node* left;
    Node* right;
    Node* next;

    Node() : val(0), left(NULL), right(NULL), next(NULL) {}

    Node(int _val) : val(_val), left(NULL), right(NULL), next(NULL) {}

    Node(int _val, Node* _left, Node* _right, Node* _next)
        : val(_val), left(_left), right(_right), next(_next) {}
};
*/

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

lc.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;
    }
};

lc.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 result = outside && inside;
        return result;
    }

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

你可能感兴趣的:(算法,leetcode)