Day16 二叉树part03 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数 559.N叉树的最大深度

二叉树part03 104.二叉树的最大深度 111.二叉树的最小深度 222.完全二叉树的节点个数 559.N叉树的最大深度

104. 二叉树的最大深度

方法一:后续递归法

class Solution {
public:
    int getHeight(TreeNode* node)
    {
        if(node==nullptr) return 0;
        int rightnode = getHeight(node->right);
        int leftnode = getHeight(node->left);
        int height = 1+max(leftnode,rightnode);
        return height;
    }
    int maxDepth(TreeNode* root) {
        int result = getHeight(root);
        return result;
    }
};

方法二:精简后续递归法

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

方法三:前序递归法

class Solution {
public:
    int result = 0; //全局变量记录深度最大值
    void getDepth(TreeNode*node ,int depth){
        result = result>depth?result:depth;
        if(!node->left&&!node->right) return; //递归结束条件
        if(node->left){
            getDepth(node->left,depth+1);
        }
        if(node->right){
            getDepth(node->right,depth+1);
        }
    }

    int maxDepth(TreeNode* root) {
        if(root==nullptr) return result;
        getDepth(root,1);
        return result;
    }
};

方法四:层序迭代

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

111. 二叉树的最小深度

方法一:后续递归法

class Solution {
public:
    int getMinHeight(TreeNode* node)
    {
        if(node==nullptr) return 0;
        int leftnode = getMinHeight(node->left);
        int rightnode = getMinHeight(node->right);
        if(!node->left&&node->right) return 1+rightnode; //左子树空,右子树不为空
        if(!node->right&&node->left) return 1+leftnode;//右子树空,左子树不为空
        int result = 1 + min(leftnode, rightnode);
        return result;
    }
    int minDepth(TreeNode* root) {
        int result = getMinHeight(root);
        return result;
    }
};

方法二:前序递归法

class Solution {
public:
    int result = INT_MAX;
    void getMinDepth(TreeNode* node,int depth){
        if(node==nullptr) return;
        if(node->left==nullptr&&node->right==nullptr) result = result<depth?result:depth;
        if(node->left) getMinDepth(node->left,depth+1);
        if(node->right) getMinDepth(node->right,depth+1);
    }
    int minDepth(TreeNode* root) {
        if (root == nullptr) return 0;     
        getMinDepth(root, 1);
        return result;
    }
};

方法三:层序迭代

class Solution {
public:
    int minDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if(root!=nullptr) que.push(root);
        int result=0;
        while(!que.empty()){
            int size = que.size();
            result++; // 记录最小深度
            while(size--){
                TreeNode* node = que.front();
                que.pop();
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
                if(!node->right&&!node->left) return result; //左右孩子都为空的时候,说明是最低点的一层了,退出
            }
        }
        return result;
    }
};

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

方法一:一般二叉树

class Solution {
public:
    int getNum(TreeNode* node)
    {
        if(node==nullptr) return 0;
        int leftNum = getNum(node->left); //左
        int rightNum = getNum(node->right); //右
        int result = leftNum+rightNum+1; //中
        return result;
        //上面四行可以浓缩为return getNum(node->left)+getNum(node->right)+1;
    }
    int countNodes(TreeNode* root) {
        return getNum(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(rightDepth==leftDepth) return(2<<leftDepth)-1; //位运算,2的leftDepth+1次方,注意(2<<1) 相当于2^2,所以leftDepth初始为0
        return countNodes(root->left) + countNodes(root->right) + 1;
    }
};

方法三:层序迭代

class Solution {
public:
    int countNodes(TreeNode* root) {
        queue<TreeNode*> que;
        int result=0;
        if(root!=nullptr) que.push(root);
        while(!que.empty())
        {
            int size = que.size();
            while(size--)
            {
                TreeNode* node =que.front();
                que.pop();
                result++; //每次队列pop元素,result就+1
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
            }
        }
        return result;  
    }
};

559. N 叉树的最大深度

方法一:层序迭代(最近写太多了脑子第一想法)

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

方法二:前序递归

class Solution {
public:
    int result = 0;
    void getDepth(Node* node,int depth){
        if(node==nullptr) return;
        result = result>depth?result:depth;
        for(auto childnode:node->children)
        {
            getDepth(childnode,depth+1);
        }
    }
    int maxDepth(Node* root) {
        if(root==nullptr) return result;
        getDepth(root,1);
        return result;
    }
};

方法三:后续递归

class Solution {
public:
    int getHeight(Node* node)
    {
        if(node==nullptr) return 0; 
        int depth = 0;
        for (int i = 0; i < node->children.size(); i++) {
            depth = max (depth, getHeight(node->children[i]));
        }
        return depth + 1;
    }
    int maxDepth(Node* root) {
        int result = getHeight(root);
        return result;
    }
};
  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值