• 剑指OFFER----面试题32


    面试题32 - I. 从上到下打印二叉树

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<int> levelOrder(TreeNode* root) {
            vector<int> res;
            if (!root) return res;
            queue<TreeNode*> q;
            q.push(root);
            while (!q.empty()) {
                TreeNode* cur = q.front();
                res.push_back(cur->val);
                q.pop();
                if (cur->left != nullptr) q.push(cur->left);
                if (cur->right != nullptr) q.push(cur->right);
            }
            return res;
        }
    };

    面试题32 - II. 从上到下打印二叉树 II

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int>> levelOrder(TreeNode* root) {
            vector<vector<int>> res;
            if (!root) return res;
            queue<TreeNode*> q;
            q.push(root);
            vector<int> tmp;
            while (!q.empty()) {
                int s = q.size();
                tmp.clear();
                for (int i = 0; i < s; i++) {
                    TreeNode* cur = q.front();
                    tmp.push_back(cur->val);
                    q.pop();
                    if (cur->left != nullptr) q.push(cur->left);
                    if (cur->right != nullptr) q.push(cur->right);
                }
                if (!tmp.empty()) res.push_back(tmp);
            }
            return res;
        }
    };

    面试题32 - III. 从上到下打印二叉树 III

    代码:

    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        vector<vector<int>> levelOrder(TreeNode* root) {
            vector<vector<int>> res;
            if (!root) return res;
    
            queue<TreeNode*> q;
            q.push(root);
            q.push(nullptr);
    
            vector<int> level;
            bool zigzag = false;
            while (q.size()) {
                auto t = q.front();
                q.pop();
                if (!t) {
                    if (level.empty()) break;
                    if (zigzag) reverse(level.begin(), level.end());
                    res.push_back(level);
                    level.clear();
                    q.push(nullptr);
                    zigzag = !zigzag;
                    continue;
                }
                level.push_back(t->val);
                if (t->left) q.push(t->left);
                if (t->right) q.push(t->right);
            }
            return res;
        }
    };
  • 相关阅读:
    SpringBoot Jpa 双数据源mysql + oracle + liquibase+参考源码
    C#:将字符串中连续空格作为分隔符获取多段模糊查询的字符串
    C# 传入参数2021-05-18T00:00:00.000Z使用ToDateTime日期在此基础上加8小时
    修改DbContext并不是线程安全的bug处理。
    产品经理推荐书籍
    抽象类、类和接口
    git 分支合并主干出现冲突的解决办法
    HttpClient请求设置Content-Type标头空格问题
    C# 3Des加密解密
    WPF 颜色选择器
  • 原文地址:https://www.cnblogs.com/clown9804/p/12380475.html
Copyright © 2020-2023  润新知