代码随想录算法训练营第16天 104.二叉树的最大深度 (优先掌握递归) 111.二叉树的最小深度 (优先掌握递归) 222.完全二叉树的节点个数(优先掌握递归)

  1. 二叉树的最大深度

链接:

https://leetcode.cn/problems/maximum-depth-of-binary-tree/

概念:

什么是高度和深度?

  • 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
  • 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)

思路:

使用递归解决

参数为节点

出口为node 为空,单层逻辑为后续遍历

操作为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 deepth(TreeNode* node)
    {
        if(node== NULL)
        {
            return 0;
        }
        int left_length=deepth(node->left);
        int right_length=deepth(node->right);
        int lenth= 1+  max(left_length,right_length);
        return  lenth;
    }

    int maxDepth(TreeNode* root) {
        if(!root) return 0;
        int length=deepth(root);
        return length;
    }
};

111.二叉树的最小深度 (优先掌握递归)

链接:

力扣

思路:

使用递归解决:

参数为节点

出口为node 为空,为后续遍历

单层逻辑为:

如果左不为空,返回左深度

如果右不为空,返回右深度

如果都不为空,返回 左右节点的最小值 加一

如果都为空,已作为0返回。

题解:

递归法:

/**
 * 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  deepth(TreeNode * node)
    {
        if(node == NULL) return 0;
        int result;
        int left=deepth(node->left);
        int right=deepth(node->right); 
        
        if(node->left == NULL && node->right!=NULL)
        {
            return  1+right;  
        }

        if(node->right == NULL && node->left!=NULL)
        {
            return  1+left;  
        }
        return 1+min(left,right);
        //result
    }
    int minDepth(TreeNode* root) {
        if(!root)   return 0;
        int result=deepth(root);
        return result;
    }
};

迭代法:

思路:

每一层深度加一,如果出队列的 节点的左右节点为空,则返回深度

代码:

/**
 * 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) {
        queue<TreeNode*> que;
        if(root) que.push(root);
        int depth;
        while(!que.empty())
        {
            int size=que.size();
            vector<int> vec;
             depth++; // 记录最小深度
            while(size--)
            {
                TreeNode*node=que.front();
                que.pop();
                if(node->left) que.push(node->left);
                if(node->right) que.push(node->right);
                if(!node->left && !node->right )
                {
                    return depth;
                }

            }
        }
        return depth;
    }
};

完全二叉树的节点个数:

链接:

https://leetcode.cn/problems/count-complete-tree-nodes/

思路:

使用递归解决:

参数,node节点。

退出条件:节点为空/左右节点遍历到顶

单层逻辑:

后序遍历:得到左边节点的高度,得到右边节点的高度,如果左右节点个数相等,

用 2^n-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 getnum(TreeNode* node)
    {

        if(node ==NULL) return 0;
        int left_num=0,right_num=0;
        TreeNode* leftnode;
        TreeNode* righnode;
        righnode=node->right;
        leftnode=node->left;
        while(leftnode)
        {
                leftnode=leftnode->left;
                left_num++;              
            
        }
        while(righnode)
        {
                righnode=righnode->right;
                right_num++;
            
        }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值