算法刷题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;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值