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

104. 二叉树的最大深度

104. 二叉树的最大深度

C++:队列

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

 C++:回溯+递归

class Solution {
public:
    int res;
    void Depth(TreeNode* root,int depth){
        res=res>depth?res:depth;
        if(root->left==nullptr&&root->right==nullptr) return;
        if(root->left) Depth(root->left,depth+1);
        if(root->right) Depth(root->right,depth+1);
        return;
    }

    int maxDepth(TreeNode* root) {
        res=0;
        if(root==nullptr) return res;
        Depth(root,1);
        return res;
    }
};

C++:

class solution {
public:
    int maxdepth(treenode* root) {
        if (root == null) return 0;
        return 1 + max(maxdepth(root->left), maxdepth(root->right));
    }
};

C#:队列

public class Solution {
    public int MaxDepth(TreeNode root) {
        if(root==null) return 0;
        Queue<TreeNode> que=new Queue<TreeNode>();
        bool flag=false;
        int res=0;
        que.Enqueue(root);
        while(que.Count!=0){
            int n=que.Count;
            for(int i=0;i<n;i++){
                TreeNode node =que.Dequeue();
                if(node.left!=null)
                    que.Enqueue(node.left);
                if(node.right!=null)
                    que.Enqueue(node.right);
                if(flag==true) 
                    res++;
            }
            res++;
        }
       return res;
    }
}

111. 二叉树的最小深度

111. 二叉树的最小深度

C++:队列

需要注意判断在循坏里面

class Solution {
public:
    int minDepth(TreeNode* root) {
        int res=INT_MAX;int temp=0;
        if(root==nullptr) return NULL;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            int n=que.size();

            temp++;
            for(int i=0;i<n;i++){
                 TreeNode* node=que.front();
                que.pop();
                if(node->left) que.push(node->left);
                //else que.push(nullptr);
                if(node->right) que.push(node->right);
                //else que.push(nullptr);
                if(node->left==nullptr&&node->right==nullptr){
                res=res<temp?res:temp;
                }
            }
        }
        return res;
    }
};

C++:回溯+递归

class Solution {
public:
    int res=INT_MAX;
    void Depth(TreeNode* root,int depth){
        if(root==nullptr) return;
        if(root->left==nullptr&&root->right==nullptr){
            res=res<depth?res:depth;
        }

        if(root->left!=nullptr) Depth(root->left,1+depth);     
        if(root->right!=nullptr) Depth(root->right,1+depth);

        return;
    }

    int minDepth(TreeNode* root) {
        if(root==nullptr) return 0;
        Depth(root,1);
        return res;
    }
};

C#:队列

class Solution {
public:
    int minDepth(TreeNode* root) {
        int res=INT_MAX;int temp=0;
        if(root==nullptr) return NULL;
        queue<TreeNode*> que;
        que.push(root);
        while(!que.empty()){
            int n=que.size();

            temp++;
            for(int i=0;i<n;i++){
                 TreeNode* node=que.front();
                que.pop();
                if(node->left) que.push(node->left);
                //else que.push(nullptr);
                if(node->right) que.push(node->right);
                //else que.push(nullptr);
                if(node->left==nullptr&&node->right==nullptr){
                res=res<temp?res:temp;
                }
            }
            
        }
        return res;
    }
};

 

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

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

C++:

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

C++:迭代

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

C#:

public class Solution {
    public int CountNodes(TreeNode root) {
        if(root==null) return 0;
        int res=0;
        Queue<TreeNode> que=new Queue<TreeNode>();
        que.Enqueue(root);
        while(que.Count!=0){
            TreeNode node=que.Dequeue();
            res++;
            if(node.left!=null) que.Enqueue(node.left);
            if(node.right!=null) que.Enqueue(node.right);
        }
        return res;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值