Day14 二叉树深度 | LeetCode 104. 二叉树的最大深度, 111. 二叉树的最小深度,222. 完全二叉树的节点个数

文章介绍了如何使用递归和迭代方法解决LeetCode中的两个二叉树问题:104题求最大深度,111题求最小深度,以及222题计算完全二叉树节点个数。分别展示了深度优先搜索和广度优先搜索在这些问题中的应用。
摘要由CSDN通过智能技术生成

LeetCode 104. 二叉树的最大深度

1)递归 (深度优先搜索)
class Solution {
public:
    int maxDepth(TreeNode* root) {
        if (root == nullptr) {
            return 0;
        }
        int leftMax = maxDepth(root->left);
        int rightMax = maxDepth(root->right);
        return max(leftMax, rightMax) + 1;
    }
};
2)迭代(广度优先搜索)
class Solution {
public:
    int maxDepth(TreeNode* root) {
        int ans = 0;
        if (!root) {
            return ans;
        }
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                root = que.front();
                que.pop();
                if (root->left) {
                    que.push(root->left);
                }
                if (root->right) {
                    que.push(root->right);
                }
            }
            ans++;
        }
        return ans;
    }
};

LeetCode 111. 二叉树的最小深度

1.递归(深度优先搜索)
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) {
            return 0;
        }
        if (!root->left && !root->right) {
            return 1;
        }
        int dep_min = INT_MAX;
        if (root->left) {
            dep_min = min(minDepth(root->left), dep_min);
        }
        if (root->right) {
            dep_min = min(minDepth(root->right), dep_min);
        }
        return dep_min + 1;
    }
};
2.迭代(广度优先搜索)
class Solution {
public:
    int minDepth(TreeNode* root) {
        if (!root) {
            return 0;
        }
        int ans = 1;
        queue<TreeNode*> que;
        que.push(root);
        while (!que.empty()){
            int size = que.size();
            while (size--) {
                root = que.front();
                que.pop();
                if (!root->left && !root->right) {
                    return ans;
                }
                if (root->left) {
                    que.push(root->left);
                }
                if (root->right){
                    que.push(root->right);
                }
            }
            ans++;
        }
        return ans;
    }
};

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

1)遍历所有节点
class Solution {
public:
    int countNodes(TreeNode* root) {
        return root == nullptr ? 0 : countNodes(root->left) + countNodes(root->right) + 1; 
    }
};
2)判断中间是否有满二叉树,非遍历所有节点
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (!root) {
            return 0;
        }
        TreeNode* leftNode = root->left;
        TreeNode* rightNode = root->right;
        int leftDepth = 0, rightDepth = 0;
        while (leftNode){
            leftDepth++;
            leftNode = leftNode->left;
        }
        while (rightNode) {
            rightDepth++;
            rightNode = rightNode->right;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1;
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值