【代码随想录】day16

本文详细介绍了使用迭代和递归两种方法求解二叉树和n叉树的最大深度、最小深度,以及完全二叉树的节点个数。重点强调了两种方法的区别和应用场景。
摘要由CSDN通过智能技术生成

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


一、104二叉树的最大深度

迭代法:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int depth = 0;
        if (root == nullptr) return depth;
        queue<TreeNode*> q;
        q.push(root);
        while (! q.empty()) {
            int n = q.size();
            while (n --) {
                TreeNode* node = q.front();
                q.pop();
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
            depth ++;
        }
        return depth;
    }
};

递归法:

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

二、559n叉树的最大深度

迭代法:

class Solution {
public:
    int maxDepth(Node* root) {
        int depth = 0;
        if (root == nullptr) {
            return depth;
        }
        queue<Node*> q;
        q.push(root);
        while (! q.empty()) {
            int n = q.size();
            while (n --) {
                Node* node = q.front();
                q.pop();
                for (auto child: node->children) {
                    if (child != nullptr) {
                        q.push(child);
                    }
                }
            }
            depth ++;
        }
        return depth;
    }
};

递归法:

class Solution {
public:
    int getDepth(Node* node) {
        int res = 0;
        if (node == nullptr) {
            return res;
        }
        for (auto child: node->children) {
            int childDepth = getDepth(child);
            res = max(res, childDepth);
        }
        return 1 + res;
    }

    int maxDepth(Node* root) {
        return getDepth(root);        
    }
};

三、111二叉树的最小深度

迭代法:

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

递归法:

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == nullptr) {
            return 0;
        }
        int leftDepth = getDepth(node->left);
        int rightDepth = getDepth(node->right);
        if (node->left == nullptr) return 1 + rightDepth;
        if (node->right == nullptr) return 1 + leftDepth;
        return 1 + min(leftDepth, rightDepth);
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

四、222完全二叉树的节点个数

迭代法:

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

递归法:

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

以上两种写法都是当成普通二叉树解题,没有用到完全二叉树的特性。通过判断子树是不是满二叉树,利用公式计算节点数可以少遍历一些节点。

class Solution {
public:
    int recur(TreeNode* node) {
        if (node == nullptr) return 0;
        TreeNode* left = node->left;
        TreeNode* right = node->right;
        int leftDepth = 0, rightDepth = 0;
        while (left != nullptr) {
            left = left->left;
            leftDepth ++;
        }
        while (right != nullptr) {
            right = right->right;
            rightDepth ++;
        }
        if (leftDepth == rightDepth) return (2 << rightDepth) - 1;
        return 1 + recur(node->left) + recur(node->right);
    }
    int countNodes(TreeNode* root) {
        return recur(root);
    }
};

总结

迭代法比较好想,递归法主要是要分清是遍历的顺序。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值