代码随想录|day16|二叉树篇----● 104.二叉树的最大深度 559.n叉树的最大深度● 111.二叉树的最小深度● 222.完全二叉树的节点个数

总链接:day 16 第六章二叉树

 104.二叉树的最大深度

链接:代码随想录

/**
 * 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==nullptr)return 0;
        else
        {
            return max(maxDepth(root->left),maxDepth(root->right))+1;
        }

    }
};

559.n叉树的最大深度

链接:力扣

 

 

/**
 * 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==nullptr)return 0;
        else
        {
            return max(maxDepth(root->left),maxDepth(root->right))+1;
        }

    }
};

111.二叉树的最小深度

链接:代码随想录

 注:这道题我用了回溯法,但并不是最佳解法。这种想法把空左子树、空右子树设为了最大。

/**
 * 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==nullptr)
        {
            return 0;
        }
        else if(root->right==nullptr && root->left==nullptr)
        {
            return 1;
        }
        return Dfs(root);

    }
    int Dfs(TreeNode *root)
    {
        if(root==nullptr)
        {
            return 100005;
        }
        
        if(root!=nullptr && root->left==nullptr && root->right==nullptr)
        {
            return 1;
        }
        int l=Dfs(root->left);
        int r=Dfs(root->right);
        return min(l,r)+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 minn=10e5+1;
    int depth=0;
    int minDepth(TreeNode* root) {
        if(root==nullptr)
        {
            return 0;
        }
        else
        {
           helper(root);
           return minn+1;
        }
        
    

    }
    void helper(TreeNode *root)
    {
        if(root==nullptr)
        {
            return ;
        }
        else if(root->left==nullptr && root->right==nullptr)
        {
              
              if(depth<minn)
              {
                  minn=depth;
              }     
        }
        else
        {
            depth++;
            helper(root->left);
            helper(root->right);
            depth--;
           

        }
    }
};

最解解法,后序遍历

class Solution {
public:
    int getDepth(TreeNode* node) {
        if (node == NULL) return 0;
        int leftDepth = getDepth(node->left);           // 左
        int rightDepth = getDepth(node->right);         // 右
                                                        // 中
        // 当一个左子树为空,右不为空,这时并不是最低点
        if (node->left == NULL && node->right != NULL) { 
            return 1 + rightDepth;
        }   
        // 当一个右子树为空,左不为空,这时并不是最低点
        if (node->left != NULL && node->right == NULL) { 
            return 1 + leftDepth;
        }
        int result = 1 + min(leftDepth, rightDepth);
        return result;
    }

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

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

链接:代码随想录

 注这道题完全忘记了怎么优化!!

/**
 * 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==nullptr)return 0;
         沿最左侧和最右侧分别计算高度
        TreeNode *l=root;
        int leftdepth=0;
        TreeNode  *r=root;
        int rightdepth=0;

        while(l!=nullptr)
        {
            l=l->left;
            leftdepth++;
        }
        while(r!=nullptr)
        {
            r=r->right;
            rightdepth++;
        }
        // 如果左右侧计算的高度相同,则是一棵满二叉树
        if(leftdepth==rightdepth)
        {
            return pow(2,leftdepth)-1;
        }
        // 如果左右侧的高度不同,则按照普通二叉树的逻辑计算
        return countNodes(root->left)+countNodes(root->right)+1;

    }
};

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值