关于层序遍历的十道题,续day15

关于层序遍历的十道题

102. 二叉树的层序遍历

时间复杂度:O(N),空间复杂度:O(N)

第一想法:使用队列遍历每一层即可

困难:没有使用固定size,因为q.size是不断变化的

class Solution {
public:
    vector<vector<int>> levelOrder(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        vector<vector<int>> ans;
        while(!q.empty())
        {
            vector<int> tmp;
            //使用固定size,因为q.size会因为你pop和push而时刻改变
            int size = q.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                tmp.push_back(top->val);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
            ans.push_back(tmp);
        }
        return ans;
    }
};

107. 二叉树的层序遍历 II

时间复杂度:O(N),空间复杂度:O(N)

第一想法:反转102的数组即可

class Solution {
public:
    vector<vector<int>> levelOrderBottom(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        vector<vector<int>> ans;
        while(!q.empty())
        {
            vector<int> tmp;
            //使用固定size,因为q.size会因为你pop和push而时刻改变
            int size = q.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                tmp.push_back(top->val);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
            ans.push_back(tmp);
        }
        reverse(ans.begin(), ans.end());
        return ans;
    }
};

199. 二叉树的右视图

时间复杂度:O(N),空间复杂度:O(N)

第一想法:从右边能看到的元素就是每一层的最后一个元素,把它插入进数组即可

class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        vector<int> ans;
        while(!q.empty())
        {
            int size = q.size();
            for(int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                //插入每层最后一个元素
                if(i == (size-1)) ans.push_back(top->val);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
        }
        return ans;
    }
};

637. 二叉树的层平均值

时间复杂度:O(N),空间复杂度:O(N)

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

429. N 叉树的层序遍历

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    vector<vector<int>> levelOrder(Node* root) {
        queue<Node*> q;
        if(root != nullptr) q.push(root);
        vector<vector<int>> ans;
        while(!q.empty())
        {
            int size = q.size();
            vector<int> tmp;
            for(int i = 0; i < size; i++)
            {
                Node* top = q.front();
                q.pop();
                tmp.push_back(top->val);
                //遍历children插入子节点即可
                for(int j = 0; j < top->children.size(); j++)
                    q.push(top->children[j]);
            }
            ans.push_back(tmp);
        }
        return ans;
    }
};

515. 在每个树行中找最大值

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    vector<int> largestValues(TreeNode* root) {
        queue<TreeNode*> q;
        if(root != nullptr) q.push(root);
        vector<int> ans;
        while(!q.empty())
        {
            int size = q.size();
            //取整数最小值
            int maxNum = INT_MIN;
            for(int i = 0; i < size; i++)
            {
                TreeNode* top = q.front();
                q.pop();
                maxNum = max(maxNum, top->val);
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
            ans.push_back(maxNum);
        }
        return ans;
    }
};

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

时间复杂度:O(N),空间复杂度:O(N)

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> q;
        if(root != NULL) q.push(root);
        while(!q.empty())
        {
            int size = q.size();
            for(int i = 0; i < size; i++)
            {
                Node* top = q.front();
                q.pop();
                //将每层的左子树的next设置为右子树
                if(i < (size-1)) top->next = q.front();
                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right);
            }
        }
        return root;
    }
};

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

时间复杂度:O(N),空间复杂度:O(N)

和116的解题思路是一样的。

class Solution {
public:
    Node* connect(Node* root) {
        queue<Node*> q;
        if(root != NULL) q.push(root);
        while(!q.empty())
        {
            int size = q.size();
            for(int i = 0; i < size; i++)
            {
                Node* top = q.front();
                q.pop();
                if(i < (size-1)) top->next = q.front();

                if(top->left) q.push(top->left);
                if(top->right) q.push(top->right); 
            }
        }
        return root;
    }
};

104. 二叉树的最大深度

111. 二叉树的最小深度

在day16中,我们已经做过了

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值