DAY16 二叉树的最大深度 二叉树的最小深度 完全二叉树的节点个数

文章讲述了如何使用递归和层序遍历方法计算二叉树的最大深度和最小深度,以及如何在完全二叉树中计算节点个数,包括两种不同的解决方案。
摘要由CSDN通过智能技术生成

104.二叉树的最大深度

(我确实不太会递归)

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例: 给定二叉树 [3,9,20,null,null,15,7],

104. 二叉树的最大深度

返回它的最大深度 3 。

说实话,这题层序遍历非常简单,代码如下:

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

递归的话建议后序遍历,因为前序和中序有一步需要还原,而后序只用从最底下往上即可

class Solution {
public:
   int countdepth(TreeNode* root)
   { 
      if(root==NULL)return 0;
      int left=countdepth(root->left);
      int right=countdepth(root->right);
      int depth=1+max(left,right);
      return depth;

   }
    int maxDepth(TreeNode* root) {
           int depth=countdepth(root);
            return depth;
    }
};

111.二叉树的最小深度

给定一个二叉树,找出其最小深度。

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

说明: 叶子节点是指没有子节点的节点。

示例:

给定二叉树 [3,9,20,null,null,15,7],

111.二叉树的最小深度1

返回它的最小深度 2

这题也是层序遍历很简单:

class Solution {
public:
    int minDepth(TreeNode* root) {
          queue<TreeNode*> qu;
            int size=0;
            int depth=0;
            if(root!=NULL)qu.push(root);
            while(!qu.empty())
            {
                size=qu.size();
               for(int i=0;i<size;i++)
                {   auto tempnode=qu.front();
                    qu.pop();
                    if(tempnode->left==NULL&&tempnode->right==NULL)return depth+1;
                    if(tempnode->left)qu.push(tempnode->left);
                    if(tempnode->right)qu.push(tempnode->right);
                }
                depth++;
            }
            return depth;
    }
};

这个和刚刚的递归不太一样,注意最小深度需要从“叶子”结点出发才行

class Solution {
public:
     int depth=0;
     int countmindepth(TreeNode* root)
     {
      if(root==NULL)return 0;
      int left=countmindepth(root->left);
      int right=countmindepth(root->right);
      if(root->left==NULL&&root->right!=NULL)
      depth=1+right;
      else if(root->left!=NULL&&root->right==NULL)
      depth=1+left;
      else 
      depth=1+min(left,right);
      return depth;
     }
    int minDepth(TreeNode* root) {
           
            return countmindepth(root);
    }
};

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

给出一个完全二叉树,求出该树的节点个数。

示例 1:

  • 输入:root = [1,2,3,4,5,6]
  • 输出:6

示例 2:

  • 输入:root = []
  • 输出:0

示例 3:

  • 输入:root = [1]
  • 输出:1

提示:

  • 树中节点的数目范围是[0, 5 * 10^4]
  • 0 <= Node.val <= 5 * 10^4
  • 题目数据保证输入的树是 完全二叉树

这个直接递归和层序遍历都可以做,但是达不到最优,需要注意“完全二叉树”条件 

class Solution {
public:
    int countNodes(TreeNode* root) {
          queue<TreeNode*> qu;
          int size=0;
          int sum=0;
          if(root!=NULL)qu.push(root);
          while(!qu.empty())
          {
              size=qu.size();
              sum+=size;
              while(size--)
              {
                  auto Temp=qu.front();
                  qu.pop();
                  if(Temp->left)qu.push(Temp->left);
                   if(Temp->right)qu.push(Temp->right);
              }
          }
          return sum;
    }
};
class Solution {
public:
     int nums=0;
    int count(TreeNode* root)
    {
        if(root==NULL)return nums;
        nums++;
        count(root->left);
        count(root->right);
        return nums;
    }

    int countNodes(TreeNode* root) {
         
          return count(root);
    }
};

 最后这个面试的时候建议直接背:

class Solution {
public:
    int count(TreeNode* root)
    {  if(root==NULL)return 0;
       auto left=root->left;
       auto right=root->right;
       int leftlength=0;
       int rightlength=0;
       while(left!=NULL)
       {
           leftlength++;
           left=left->left;
       }
       while(right!=NULL)
       {
           rightlength++;
           right=right->right;
       }
       if(leftlength==rightlength)return (2<<leftlength)-1;
       int left1=count(root->left);
       int right1=count(root->right);
       return 1+left1+right1;
    }

    int countNodes(TreeNode* root) {
          return count(root);
    }
};

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值