代码随想录第14天|二叉树操作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:
    // 1.确定参数:参数只需要指针
    int maxDepth(TreeNode* root) {
        // 2.判断递归终止条件,遇到空指针就返回
        if(root == nullptr) return 0;
        // 3.确定递归逻辑,使用后续遍历访问顺序,获取左子树和右子树中的最大高度,取最大值
        int left = maxDepth(root->left);
        int right = maxDepth(root->right);
        return max(left,right) + 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 res = 0;
    // 前序遍历
    void getDepth(TreeNode* cur, int depth) {
        res = max(res, depth);
        if (cur->left == nullptr && cur->right == nullptr) {
            return;
        }
        if (cur->left)
            getDepth(cur->left, depth + 1);
        if (cur->right)
            getDepth(cur->right, depth + 1);
        return;
    }
    // 1.参数只需要指针
    int maxDepth(TreeNode* root) {
        if (root == nullptr)
            return 0;
        getDepth(root, 1);
        return res;
    }
};

二叉树的最小深度

在这里插入图片描述

迭代法上一篇文章里有,这里使用递归法
当遍历到空节点的时候返回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 getDepth(TreeNode* cur){
        if(cur == nullptr) return 0;
        int left = getDepth(cur->left);
        int right = getDepth(cur->right);
        if(cur->left == nullptr && cur->right != nullptr) return 1 + right;
        if(cur->left != nullptr && cur->right == nullptr) return 1 + left;
        return 1 + min(left,right);
    }
    int minDepth(TreeNode* root) {
        return getDepth(root);
    }
};

可以思考另一种方式,当节点不是叶子节点的时候需要特判,那么我们直接对于叶子节点返回,就可以不写判断的代码。但是需要判断其左右孩子是否存在(因为不是遍历到空节点的时候就能返回正确的答案)

/**
 * 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* cur) {
        if (cur->left == nullptr && cur->right == nullptr)
            return 1;
        int left = INT_MAX;
        int right = INT_MAX;
        if (cur->left)
            left = getDepth(cur->left);
        if (cur->right)
            right = getDepth(cur->right);
        return 1 + min(left, right);
    }
    int minDepth(TreeNode* root) {
        if (root == nullptr)
            return 0;
        return getDepth(root);
    }
};

完全二叉树的节点数量

在这里插入图片描述
层序遍历求个数

/**
 * 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;
        queue<TreeNode*> q;
        int num = 0;
        q.push(root);
        while (!q.empty()) {
            TreeNode* cur = q.front();
            q.pop();
            num++;
            if (cur->left)
                q.push(cur->left);
            if (cur->right)
                q.push(cur->right);
        }
        return num;
    }
};

后序遍历求个数,先求左子树的节点数,再求右子树的节点数,最后返回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 countNodes(TreeNode* root) {
        if (root == nullptr)
            return 0;
        int left = countNodes(root->left);
        int right = countNodes(root->right);
        return 1 + left + right;
    }
};
  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值