代码随想录算法训练营第十六天|104.二叉树的最大深度、559.n叉树的最大深度、111.二叉树的最小深度、222.完全二叉树的节点个数

104.二叉树的最大深度

1.第一想法是,层序遍历。

2.前后序遍历,但其实有些细节没理解,depth是如何更新的(迭代法),同时注意递归法的三原则(重要重要)。

层序:

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root == nullptr) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int result = 0;
        while(!que.empty()){
            int 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);    
            }
            result++;

        }
        return result;
    }
};

递归:

class Solution {
public:

   int getDepth(TreeNode* node){
       if (node == nullptr) return 0;
       int leftdepth = getDepth(node->left);
       int rightdepth = getDepth(node->right);
       int depth = 1 + max(leftdepth, rightdepth);
       return depth;
       
   }

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

559.n叉树的最大深度

1.自己尝试使用递归法完成

2.层序遍历、

自己思路:

class Solution {
public:
    int getDepth(Node* node){
        int res = 0;
        if(node == nullptr) return res;
        for(int i = 0; i < node->children.size(); i++){
            res = max(res, getDepth(node->children[i]));
        }
        return res + 1;
    }

    int maxDepth(Node* root) {

        return getDepth(root);
        
    }
};

层序遍历

class Solution {
public:

    int maxDepth(Node* root) {
        queue<Node*> que;
        int result = 0;
        if(root == nullptr) return result;
        que.push(root);
        while(!que.empty()){
            int size = que.size();
            for(int i = 0; i < size; i++){
                Node* node = que.front(); 
                que.pop();
                for(int j = 0; j < node->children.size(); j++){
                   if (node->children[j]) que.push(node->children[j]);
                }
                
            }
            result++;
        }
        return result;    
    }
};

111.二叉树的最小深度

1.自己尝试使用递归法完成

2.层序遍历、前序遍历

自己思路:

class Solution {
public:

    int findDepth(TreeNode* node){
        if(node == nullptr) return 0;
        int leftDepth = findDepth(node->left);
        int rightDepth = findDepth(node->right);

        if(node->left == nullptr && node->right != nullptr){
            return 1 + rightDepth;
        }

        if(node->left != nullptr && node->right == nullptr){
            return 1 + leftDepth;
        }

        int result = 1 + min(leftDepth, rightDepth);
        return result;

    }

    int minDepth(TreeNode* root) {

       return findDepth(root);

    }
};

层序遍历:

class Solution {
public:

    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int depth = 0;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()) {
            int size = que.size();
            depth++; // 记录最小深度
            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);
                if (!node->left && !node->right) { // 当左右孩子都为空的时候,说明是最低点的一层了,退出
                    return depth;
                }
            }
        }
        return depth;
    }
};

前序遍历:

class Solution {
private:
    int result;
    void getdepth(TreeNode* node, int depth) {
        if (node->left == NULL && node->right == NULL) {
            result = min(depth, result);  
            return;
        }
        // 中 只不过中没有处理的逻辑
        if (node->left) { // 左
            getdepth(node->left, depth + 1);
        }
        if (node->right) { // 右
            getdepth(node->right, depth + 1);
        }
        return ;
    }

public:
    int minDepth(TreeNode* root) {
        if (root == NULL) return 0;
        result = INT_MAX;
        getdepth(root, 1);
        return result;
    }
};

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

1.一开始的想法是先找到数的深度,然后通过树的深度可计算出除最后一层外的节点数,但不知道最后一层的节点该如何计算。然后想到层序遍历,直接计算,但感觉应有优化办法。

2.递归方法;注意题目中提到的利用完全二叉树的性质进行计算的方法。

自己思路:

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if(root == nullptr) return 0;
        que.push(root);
        int result = 0;

        while(!que.empty()){
            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;
    }
};

递归法:

class Solution {
public:
    int getNodeNums(TreeNode* cur) {

        if(cur == nullptr) return 0;   
        int leftNums = getNodeNums(cur->left);
        int rightNums = getNodeNums(cur->right);
        return leftNums + rightNums + 1;
          
    } 
    int countNodes(TreeNode* root) {
        if(root == nullptr)  return 0;
        return getNodeNums(root); 
       
    }

        
};

递归法(考虑完全二叉树的性质):​​​​​​​

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root->right;
        int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
        while (left) {  // 求左子树深度
            left = left->left;
            leftDepth++;
        }
        while (right) { // 求右子树深度
            right = right->right;
            rightDepth++;
        }
        if (leftDepth == rightDepth) {
            return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
        }
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

注意:

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值