## day15 - 二叉树part02

发布时间 2023-09-25 11:23:35作者: 笑忘书丶丶

day15 - 二叉树part02

力扣102. 二叉树的层序遍历

思路:使用一个队列,将根节点放入队列,并使用size记录每一层的节点数量,然后遍历。

为什么和深度优先搜索不一样了呢?为什么不能使用递归了呢?

比如先序遍历时,每层的逻辑都是根左右,遍历到当前节点,就对当前节点实施根左右,可以完成递归。

但是层序遍历不可以,如果使用递归,那么每层的逻辑应该是遍历一层,如果遍历到第二层第一个节点时候,并不知道其他地方有几个节点,也不知道其他的节点如何获取,因为每个节点的父亲节点是不同的,因此要用队列

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

        return result;

    }
};
力扣107. 二叉树的层次遍历II

只需照上面的代码,在return之前进行一次reverse即可

力扣199. 二叉树的右视图

思路:只需要将层序遍历的每一层的最后一个节点值取出即可

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<vector<int>> result = {};
        queue<TreeNode*> que;
        if (root == nullptr)
        {
            return {};
        }
        que.push(root);
        while (!que.empty())
        {
            int size = que.size();
            vector<int> tmp;
            while (size--)
            {
                TreeNode* t = que.front();
                tmp.push_back(t->val);
                que.pop();
                if (t->left)
                {
                    que.push(t->left);
                }
                if (t->right)
                {
                    que.push(t->right);
                }
            }
            result.push_back(tmp);
        }
        vector<int> result1;
        for (int i = 0; i < result.size(); i++)
        {
            result1.push_back(result[i][result[i].size() - 1]);
        }
        return result1;

    }
};
力扣637. 二叉树的层平均值

把每层数加起来求平均值,用double不然精度不够

力扣429. N叉树的层序遍历

思路:只需把每个孩子节点push进队列即可

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        vector<vector<int>> result;
        queue<Node*> que;
        if (root)
        {
            que.push(root);
        }
        else
        {
            return {};
        }
        while (!que.empty())
        {
            int size = que.size();
            vector<int> tmp;
            while (size--)
            {
                Node* t = que.front();
                tmp.push_back(t->val);
                que.pop();
                for (int i = 0; i < t->children.size(); i++)
                {
                    que.push(t->children[i]);
                }
            }
            result.push_back(tmp);
        }
        return result;
    }
};
力扣515. 在每个树行中找最大值

设置一个max即可

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

思路:如果是遍历的当前层的第一个节点,那么记录此节点,然后后面的依次指向下一个

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

和上题一样的代码直接过了...没看出来区别

力扣104. 二叉树的最大深度

思路:每层把深度加一。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int deep = 0;
        queue<TreeNode*> que;
        if (root)
        {
            que.push(root);
        }
        else
        {
            return 0;
        }
        while (!que.empty())
        {
            int size = que.size();
            if (size != 0)
            {
                deep++;
            }
            while (size--)
            {
                TreeNode* t = que.front();
                que.pop();
                if (t->left) que.push(t->left);
                if (t->right) que.push(t->right);
            }
        }
        return deep;
    }
};
力扣111. 二叉树的最小深度

思路:定义一个深度。每一层深度加 1,如果左右子树都空,直接返回。

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int depth = 0;
        if (root)
        {
            que.push(root);
        }
        else
        {
            return 0;
        }
        while (!que.empty())
        {
            int size = que.size();
            depth++;
            while (size--)
            {
                TreeNode* t = que.front();
                que.pop();
                if (t->left) que.push(t->left);
                if (t->right) que.push(t->right);
                if (t->left == nullptr && t->right == nullptr)
                {
                    return depth;
                }
            }
        }
        return depth;
    }
};
力扣226. 翻转二叉树

递归

class Solution {
public:

    void traverse(TreeNode* node)
    {
        if (node == nullptr)
        {
            return;
        }
        TreeNode* t;
        t = node->left;
        node->left = node->right;
        node->right = t;
        traverse(node->left);
        traverse(node->right);
        return;
    }

    TreeNode* invertTree(TreeNode* root) {

        traverse(root);
        return root;
    }
};
力扣101. 对称二叉树

思路:递规,如果两边孩子是空返回真,一边空一边不空,那么返回假。如果都不空,比较值,不相等返回假。

如果相等,继续比较左孩子的左孩子,右孩子的右孩子,和左孩子的右孩子,右孩子的左孩子

class Solution {
public:

    bool cmp(TreeNode* left, TreeNode* right)
    {
        if (left == nullptr && right == nullptr) return true;
        else if (left == nullptr && right != nullptr) return false;
        else if (left != nullptr && right == nullptr) return false;
        else if (left->val != right->val) return false;
        else return (cmp(left->left, right->right) && cmp(left->right, right->left));
    }

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