算法训练day16|104. 二叉树的最大深度|559. N 叉树的最大深度

文章介绍了如何使用层序遍历和递归方法计算二叉树和N叉树的最大深度、最小深度以及完全二叉树的节点个数。对于二叉树,还涉及到最小深度的递归解决方案。文章强调了层序遍历在处理这类问题中的重要性,并指出对递归的熟练掌握同样关键。
摘要由CSDN通过智能技术生成

一、104. 二叉树的最大深度

  1. 层序遍历求最大深度

每一层i++即可,很好理解。

class Solution {
public:
    int maxDepth(TreeNode* root) {
        int i=0;
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        while (!que.empty()) {
            int size = que.size();
            // 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
            for (int i = 0; i < size; i++) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
            i++;
        }
        return i;
    }
};
  1. 后序遍历

关于递归还是有些不熟悉

class Solution {
public:
    int getMaxDepth(TreeNode* node) {
        if (node == nullptr) {
            return 0;
        }
        int left = getMaxDepth(node->left);
        int right = getMaxDepth(node->right);
        return 1 + max(left, right);
    }
    int maxDepth(TreeNode* root) {
        return getMaxDepth(root);
    }
};
  1. 先序遍历

class Solution {
public:
    int result; //用全局变量记录答案
    void getMaxDepth(TreeNode* node, int depth) { // 前序遍历
        result = result > depth ? result : depth;// 中
        if (!node->left && !node->right) {
            return;
        }
        if (node->left) { // 左
            depth++; // 深度加一
            getMaxDepth(node->left, depth);
            depth--; // 回溯
        }
        if (node->right) { // 右
            depth++; // 深度加一
            getMaxDepth(node->right, depth);
            depth--; // 回溯
        }
        return ;
    }
    int maxDepth(TreeNode* root) {
        result = 0;
        if (root == nullptr) {
            return 0;
        }
        getMaxDepth(root, 1);
        return result;
    }
};

二、559. N 叉树的最大深度

  1. 层序遍历 (迭代)

class Solution {
public:
    int maxDepth(Node* root) { // 层序遍历
        queue <Node*> que;
        if (root) {
            que.push(root);
        }
        int i = 0;
        
        while (que.empty() != true) {
            int size = que.size();
            i++;
            for(int i=0; i<size; i++) {
                Node* node = que.front();
                que.pop(); // 当前队头出队
                for(int j=0; j<node->children.size(); j++) {
                    que.push(node->children[j]); // 把所有孩子入队
                }
            }
        }
        return i;
    }
};

目前而言层序遍历已经非常熟悉了,还要多练习递归。

  1. 递归

class Solution {
public:
    int maxDepth(Node* root) { // 先根遍历
        if (!root) {
            return 0;
        }
        int depth = 0;
        for (int i=0; i < root->children.size(); i++) {
            depth = max(depth, maxDepth(root->children[i]));
        }
        return 1 + depth;
    }
};

三、111. 二叉树的最小深度

  1. 层序遍历

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int cnt = 0;
        while (!que.empty()) {
            int size = que.size();
            TreeNode* node;
            cnt++;
            // 这里一定要使用固定大小size,不要使用que.size(),因为que.size是不断变化的
            for (int i = 0; i < size; i++) {
                node = que.front();
                que.pop();
                if(!node->left && !node->right) return cnt;
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return cnt;
    }
};
  1. 递归

最小深度是从根节点到最近叶子节点的最短路径上的节点数量!!!

class Solution {
public:
    int getMinDepth(TreeNode* node) {
        if (node == nullptr) {
            return 0;
        }
        int left = getMinDepth(node->left);
        int right = getMinDepth(node->right);
        // 左子树为空, 右子树非空, 则返回右子树的深度 + 1
        if (node->left == nullptr && node->right) {
            return 1 + right;
        }
        // 右子树为空, 左子树非空, 则返回左子树的深度 + 1
        if (node->left && node->right == nullptr) {
            return 1 + left;
        }
        return 1 + min(left, right);
    }
    int minDepth(TreeNode* root) {
        return getMinDepth(root);
    }
};

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

  1. 层序遍历

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue <TreeNode*> que;
        int result = 0;
        if (root) {
            que.push(root);
        }
        while (que.empty() != true) {
            int size = que.size();
            for(int i=0; i<size; i++) {
                TreeNode* node = que.front();
                que.pop();
                result++;
                if (node->left) {
                    que.push(node->left);
                }
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return result;
    }
};
  1. 递归

全局变量

class Solution {
public:
    int result;
    void preOrder(TreeNode* node) {
        if (node == nullptr) {
            return ;
        }
        result++;
        preOrder(node->left);
        preOrder(node->right);
    }
    int countNodes(TreeNode* root) {
        result = 0;
        preOrder(root);
        return result;
    }
};

函数返回

class Solution {
private:
    int getNodesNum(TreeNode* cur) {
        if (cur == NULL) return 0;
        int leftNum = getNodesNum(cur->left);      // 左
        int rightNum = getNodesNum(cur->right);    // 右
        int treeNum = leftNum + rightNum + 1;      // 中
        return treeNum;
    }
public:
    int countNodes(TreeNode* root) {
        return getNodesNum(root);
    }
};
  1. 留下一个时间复杂度O(log n × log n)的二刷来学

五、总结

其实目前来说掌握几个递归和层序遍历就差不多,现在刷了这么多题已经很熟悉了。

(3小时)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值