二叉树的深度与高度

111. 二叉树的最小深度

方法:递归

class Solution {
private:
    int getDep(TreeNode* nod) {
        if (nod == NULL) return 0;
        int l = getDep(nod->left);
        int r = getDep(nod->right);
        if (nod->left == NULL && nod->right != NULL) return 1 + r;
        if (nod->right == NULL && nod->left != NULL) return 1 + l;
        int dep = min(l, r) + 1;
        return dep; 
    }
public:
    int minDepth(TreeNode* root) {
       return getDep(root);
    }
};

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

方法:bfs

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int dep = 0;
        queue q;
        q.push(root);
        while (!q.empty()) {
            int n = q.size();
            ++dep;
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
                if (!nod->left && !nod->right) return dep;
            }
        }
        return dep;
    }
};

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

104. 二叉树的最大深度

方法:递归

class Solution {
private:
    int res;
    void getDep(TreeNode* cur, int dep) {
        res = res > dep ? res : dep;
        if (cur->left == NULL && cur->right == NULL) return ;
        if (cur->left) {
            ++dep;
            getDep(cur->left, dep);
            --dep;
        }

        if (cur->right) {
            ++dep;
            getDep(cur->right, dep);
            --dep;
        }
        return ;
    }
public:
    int maxDepth(TreeNode* root) {
        res = 0;
        if (root == NULL) return res;
        getDep(root, 1);
        return res;
    }
};

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

方法:bfs

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queueq;
        if(root == NULL) return 0;
        int dep = 0;
        q.push(root);
        while(!q.empty()) {
            dep++;
            int n = q.size();
            for(int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if(nod->left) q.push(nod->left);
                if(nod->right) q.push(nod->right);
            }
        }
        return dep;
    }
};

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

222. 完全二叉树的节点个数

方法:bfs

class Solution {
public:
    int countNodes(TreeNode* root) {
        int res = 0;
        if (root == NULL) return 0;
        queue q;
        q.push(root);
        while(!q.empty()) {
            int n = q.size();
            res += n;
            for (int i = 0; i < n; ++i) {
                TreeNode* nod = q.front();
                q.pop();
                if (nod->left) q.push(nod->left);
                if (nod->right) q.push(nod->right);
            }
        }
        return res;
    }
};

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

方法:递归

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root == NULL) return 0;
        return 1 + countNodes(root->left) + countNodes(root->right);
    }
};

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

你可能感兴趣的:(algorithm,数据结构)