day16|leetcode|C++|二叉树DFS|104.二叉树的最大深度|111.二叉树的最小深度|222.完全二叉树的节点个数

本文讨论了解决LeetCode中关于二叉树的三个问题:104题最大深度采用BFS和DFS方法,111题最小深度同样用后序遍历,222题完全二叉树节点个数涉及递归和深度计算。展示了C++实现的详细代码.

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Leetcode 104.二叉树的最大深度

链接104.二叉树的最大深度

thought

  • 利用BFS(层序遍历)可解此题,可用队列,设置变量line_count,每层遍历完+1
  • 利用DFS(本题采用后序遍历),可用递归或栈
  • DFS: 此树的深度和其左(右)子树的深度之间的关系。显然,此树的深度 等于 左子树的深度右子树的深度中的 最大值 +1+1+1 。
  • 以后序为例,确定参数:根节点**;确定终止条件:节点为空;**单层递归逻辑:求左右子树高度,其中的max+1即为当前树的最大深度

完整C++代码如下:

//DFS
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (!root)
            return 0;
        int num1 = maxDepth(root->left);
        int num2 = maxDepth(root->right);
        int maxN = 1 + max(num1, num2);
        return maxN;
    };
};

//BFS
class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        int count=0;
        if (root)
            que.push(root);
        while (!que.empty()) {
            int size = que.size();
            count++;
            for (int i = 0; i < size; i++) {
                TreeNode* cur = que.front();
                que.pop();
                vec.push_back(cur->val);
                if (cur->left)
                    que.push(cur->left);
                if (cur->right)
                    que.push(cur->right);
            }
        }
        return count;
    }
};

Leetcode 111. 二叉树的最小深度

链接111. 二叉树的最小深度

thought:仍然是后序遍历,不过处理了!root->left && root->rightroot->left && !root->right两种情况

完整C++代码如下:

//DFS
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root)
            return 0;
        else
            return minD(root);
    };
    int minD(TreeNode* root) {
        if (!root->left && !root->right)
            return 1;
        else if (!root->left && root->right) {
            return 1 + minD(root->right);
        } else if (root->left && !root->right) {
            return 1 + minD(root->left);
        } else {
            return 1 + min(minD(root->left), minD(root->right));
        }
    }
};

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

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

thought

  • 本质是每轮确定一个节点,若其左右子树都为满二叉树,直接返回,反之则进行递归

  • 原理如下:

  • 如果根节点的左子树深度等于右子树深度,则说明 左子树为满二叉树

    在这里插入图片描述

  • 如果根节点的左子树深度大于右子树深度,则说明 右子树为满二叉树

    在这里插入图片描述

    原链接来自 :link 作者:左

完整C++代码如下:

class Solution {
public:
    int countLevels(TreeNode* root) {
        int level = 0;
        while (root != nullptr) {
            level++;
            root = root->left;
        }
        return level;
    }
    int countNodes(TreeNode* root) {
        int l_depth = countLevels(root);
        return count(root, l_depth - 1);
    }
    int count(TreeNode* root, int l_depth) {
        if (!root)
            return 0;
        int r_depth = countLevels(root->right);
        if (l_depth == r_depth)
            return (1 << l_depth) + count(root->right, r_depth - 1);
        else
            return (1 << r_depth) + count(root->left, l_depth - 1);
    }
};
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值