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

本文详细探讨了如何计算二叉树的最大深度,包括递归和迭代两种方法,以及如何通过层序遍历计算节点个数,特别关注完全二叉树的应用。
摘要由CSDN通过智能技术生成

104.二叉树的最大深度

1.很容易想到二叉树的深度就是其层数,选用迭代法的层序遍历,每多遍历一层深度+1

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if(root!=NULL) que.push(root);
        int deepth=0;
        while(!que.empty()){
            int size=que.size();
            for(int i=0;i<size;i++){
                TreeNode* cur=que.front();
                que.pop();
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
            deepth++;//每遍历一层深度+1
        }
        return deepth;
    }
};

2.用递归的方法也能做(简洁写法,隐藏了很多细节)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public://递归法后续遍历求深度
    int maxDepth(TreeNode* root) {
        if(root==NULL) return 0;
        return 1+max(maxDepth(root->left),maxDepth(root->right));
    }
};

3.递归的完整写法(以前序遍历为例)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public://递归法前序遍历求深度
int result;
void getDepth(TreeNode* node,int deep){
    result= result>deep? result:deep;//中
    if(node->left==NULL&&node->right==NULL) return;
    if(node->left) getDepth(node->left,deep+1);//左
    if(node->right) getDepth(node->right,deep+1);//右
    return;
}
    int maxDepth(TreeNode* root) {
        result=0;
        if(root==0) return result;
        getDepth(root,1); return result;
    }
};

559.N叉树的最大深度

1.递归法

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public://递归法
    int maxDepth(Node* root) {
        if(root==NULL) return 0;
        int deepth=0;
        for(int i=0;i<root->children.size();i++){//求出N个子树的最大深度
            deepth=max(deepth,maxDepth(root->children[i]));
        }
        return deepth+1;//子树深度+1才是总深度
    }
};

2.迭代法

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public://迭代法层序遍历
    int maxDepth(Node* root) {
        queue<Node*> que;
        if(root==NULL) return 0;
        que.push(root);
        int deepth =0;
        while(!que.empty()){
            deepth++;
            int size=que.size();
            for(int i=0;i<size;i++){
            Node* cur=que.front();
            que.pop();
            for(int j=0;j<cur->children.size();j++){
                if(cur->children[j])
                que.push(cur->children[j]);
                }
            }
        }
        return deepth;
    }
};

111.二叉树的最小深度

1.第一想法是迭代法层序遍历,深度最小的末尾结点一定是层序遍历第一个左右孩子都为空的结点

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int minDepth(TreeNode* root) {//层序遍历,找到第一个左右孩子都为空的结点并返回深度
        if(root==NULL) return 0;
        queue<TreeNode*> que;
        if(root!=NULL) que.push(root);
        int deepth=0;
        while(!que.empty()){
            deepth++;
            int size=que.size();
            for(int i=0;i<size;i++){
                TreeNode* cur=que.front();
                que.pop();
                if(!cur->left&&!cur->right) return deepth;//左右孩子都为空,到达最低层
                if(cur->left) que.push(cur->left);
                if(cur->right) que.push(cur->right);
            }
        }
        return deepth;
    }
};

2.递归法(后序遍历版)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public://递归法后序遍历
int getDepth(TreeNode* root){
    if(root==NULL) return 0;
    int leftDepth=getDepth(root->left);//左
    int rightDepth=getDepth(root->right);//右
    //中
    if(root->left==NULL&&root->right!=NULL)
    return 1+rightDepth;
    if(root->left!=NULL&&root->right==NULL)
    return 1+leftDepth;
    int result=1+min(leftDepth,rightDepth);
    return result;
}
    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

3.递归法(前序遍历版)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public://递归法(前序遍历)
int result;//全局变量
void getDepth(TreeNode* root,int deep){
    if(root==NULL) return;//递归终止条件
    if(!root->left&&!root->right)//中
    result= min(result,deep);
    if(root->left)
    getDepth(root->left,deep+1);//左
    if(root->right)//右
    getDepth(root->right,deep+1);
    return;
}
    int minDepth(TreeNode* root) {
        if(root==NULL) return 0;
        result=INT_MAX;//设置一个最大值,方便交换
        getDepth(root,1);
        return result;
    }
};

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

1.迭代法前序遍历数结点个数

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int countNodes(TreeNode* root) {
        stack<TreeNode*> st;
        int result=0;
        if (root == NULL) return result;
        st.push(root);
        while (!st.empty()) {
            TreeNode* node = st.top();                       // 中
            st.pop();
            result++;
            if (node->right) st.push(node->right);           // 右(空节点不入栈)
            if (node->left) st.push(node->left);             // 左(空节点不入栈)
        }
        return result;
    }
};

2.迭代法层序遍历数结点个数

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public://层序遍历数结点个数
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != NULL) que.push(root);
        int result=0;
        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();
                result++;
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
        }
        return result;
    }
};

3.递归法(代码非常简洁)

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public://递归写法
    int countNodes(TreeNode* root) {
        if(!root) return 0;
        return 1+countNodes(root->left)+countNodes(root->right);
    }
};

今日总结:好好好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值