算法刷题Day 16 二叉树的最大深度+N叉树的最大深度+二叉树的最小深度+完全二叉树的节点个数

104. 二叉树的最大深度

递归法

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        return max(maxDepth(root->left), maxDepth(root->right)) + 1;
    }
};

迭代法

使用层序的方法,相对比较好理解

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int maxD = 0;

        while (!que.empty())
        {
            int len = que.size();
            maxD++;
            for (int i = 0; i < len; ++i)
            {
                TreeNode *cur = que.front();
                que.pop();
                if (cur->left) que.push(cur->left);
                if (cur->right) que.push(cur->right);
            }
        }

        return maxD;
    }
};

559. N叉树的最大深度

递归法

class Solution {
public:
    int maxDepth(Node* root) {
        if (!root) return 0;
        int maxD = 0;

        for (auto child : root->children)
        {
            auto childD = maxDepth(child);
            if (maxD < childD)
            {
                maxD = childD;
            }
        }

        return maxD + 1;
    }
};

迭代法

跟二叉树的迭代差不多意思。

class Solution {
public:
    int maxDepth(Node* root) {
        if (!root) return 0;
        queue<Node*> que;
        que.push(root);
        int maxD = 0;

        while (!que.empty())
        {
            int len = que.size();
            maxD++;
            for (int i = 0; i < len; ++i)
            {
                Node *cur = que.front();
                que.pop();
                for (auto child : cur->children)
                {
                    if (child)
                    {
                        que.push(child);
                    }
                }
            }
        }

        return maxD;
    }
};

111.二叉树的最小深度

要想到是后序遍历

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        // 应该是后序遍历
        if (root->left == nullptr && root->right == nullptr) return 1;
        int minD = INT_MAX;
        if (root->left) minD = min(minD, minDepth(root->left));
        if (root->right) minD = min(minD, minDepth(root->right));
        return minD + 1;
    }
};

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

递归法

先计算左右两棵子树的节点数,再加和,用后序遍历的方法

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

迭代法

迭代法用层序遍历来处理

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (!root) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int cnt = 0;

        while (!que.empty())
        {
            int len = que.size();
            cnt += len;

            for (int i = 0; i < len; ++i)
            {
                TreeNode *cur = que.front();
                que.pop();
                if (cur->left) que.push(cur->left);
                if (cur->right) que.push(cur->right);
            }
        }

        return cnt;
    }
};

适用于完全二叉树的优化

完全二叉树优化方法没自己想出来,得多复习几遍

优化后也是递归的思想,判断当前树是否为满二叉树,如果是,直接使用公式计算节点个数,否则,递归地计算左右子树的节点个数。

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode *leftNode = root->left, *rightNode = root->right;
        int leftDepth = 0, rightDepth = 0;

        while (leftNode)
        {
            leftNode = leftNode->left;
            leftDepth++;
        }

        while (rightNode)
        {
            rightNode = rightNode->right;
            rightDepth++;
        }

        if(leftDepth == rightDepth)
        {
            return (2 << leftDepth) - 1;
        }

        int leftCnt = countNodes(root->left);
        int rightCnt = countNodes(root->right);
        return leftCnt + rightCnt + 1;
    }
};

你可能感兴趣的:(算法,leetcode,数据结构)