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

104. 二叉树的最大深度

注意点:

1.采用递归的方式,注意递归三部曲

2.树的深度和高度是不一样的,本题采用后序的方式对树进行深度遍历

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

3.注意最后的叶子节点就是1,所以depth得加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 getDepth(TreeNode* node) {//确定参数的返回类型
        if(node == nullptr) return 0;//确定递归的终止条件

        //确定单层递归逻辑
        int leftDepth = getDepth(node->left);//左
        int rightDepth = getDepth(node->right);//右
        int depth = 1+ max(leftDepth,rightDepth);//中

        return depth;

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

559. N 叉树的最大深度

参考104题解

/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> children;

    Node() {}

    Node(int _val) {
        val = _val;
    }

    Node(int _val, vector<Node*> _children) {
        val = _val;
        children = _children;
    }
};
*/

class Solution {
public:
    int getDepth(Node* node) {
        if(node == NULL) return 0;
        int depth = 0;
        for(int i = 0; i < node->children.size(); i++) {
            depth = max(depth, getDepth(node->children[i]));
        }
        return depth+1;
    }
    int maxDepth(Node* root) {
        return getDepth(root); 
    }
};

111. 二叉树的最小深度

注意点:

1.本题采用和559一样的方法,但是本题主要针对特殊情况进行了处理(如下图所示的特殊情况)

/**
 * 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 getDepth(TreeNode* node) {
        if(node == nullptr) return 0;
        int leftDepth = getDepth(node->left);//左
        int rightDepth = getDepth(node->right);//右

        // 中间进行遍历
        if(node->left == nullptr && node->right != nullptr) return 1+rightDepth;//处理实例1中没有9的情况
        if(node->left != nullptr && node->right == nullptr) return 1+leftDepth;
        int depth = 1+ min(leftDepth, rightDepth);
        return depth;
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);

    }
};

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

注意点:
1.递归三部曲-递归参数和返回值-终止条件-单层递归逻辑

2.主要针对的是对完全二叉树进行分解,将每部分分解为满二叉树 

/**
 * 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 == 0) return 0;
        TreeNode* left = root->left;
        TreeNode* right = root ->right;
        int leftLen = 0, rightLen = 0;
        while(left) {
            left = left->left;
            leftLen++;
        }
        while(right){
            right = right->right;
            rightLen++;
        }

        if(leftLen == rightLen)
            return ((2<<leftLen)-1);//leftLen=0,2-1;leftLen=1,4-1;leftLen=2,8-1
        // 单层递归逻辑
        int TreeLeftNum = countNodes(root->left);
        int TreeRightNum = countNodes(root->right);
        int result = TreeLeftNum + TreeRightNum +1;
        return result;
    }
};

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值