二叉树层次遍历

101. 对称二叉树

方法:bfs

/**
 * 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) {
        vector> res;
        
        queue q;
        if (root != NULL) q.push(root);
        while (!q.empty()) {
            vector tmp;
            int n = q.size();
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                tmp.push_back(nod->val);
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
            }
            res.push_back(tmp);
        }
        return res;
    }
};

$时间复杂度O(n),空间复杂度O(n)

方法:递归

/**
 * 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 {
private:
    void solve(TreeNode* cur, vector>& res, int dep) {
        if (cur == NULL) return ;
        if (res.size() == dep) res.push_back(vector());
        res[dep].push_back(cur->val);
        solve(cur->left, res, dep+1);
        solve(cur->right, res, dep+1);
    }
public:
    vector> levelOrder(TreeNode* root) {
        vector> res;
        solve(root, res, 0);
        return res;
    }
};

$时间复杂度O(2^{h}),空间复杂度O(logh)h为数的高度
 

226. 翻转二叉树

方法:bfs

/**
 * 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) {
        queue q;
        if (root != NULL) q.push(root);
        while (!q.empty()) {
            int n = q.size();
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                swap(nod->left, nod->right);
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
            }
        }
        return root;
    }
};

$时间复杂度O(n),空间复杂度O(n)

101. 对称二叉树

方法:bfs

/**
 * 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 isSymmetric(TreeNode* root) {
        queue q;
        q.push(root->left);
        q.push(root->right);
        while (!q.empty()) {
            TreeNode* l = q.front(); q.pop();
            TreeNode* r = q.front(); q.pop();
            if (!l && !r) continue;
            if (!l|| !r|| (l->val != r->val)) return false;
            q.push(l->left);
            q.push(r->right);
            q.push(l->right);
            q.push(r->left);
        }
        return true;
    }
};

$时间复杂度O(n),空间复杂度O(n)

你可能感兴趣的:(c++,算法,开发语言)