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

104.二叉树的最大深度

文章讲解:代码随想录 (programmercarl.com)

题目链接:104. 二叉树的最大深度 - 力扣(LeetCode)

视频讲解::二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度

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

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

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

思路:

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

后序遍历(左右中)

  1. 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型
int getdepth(treenode* node)
  1. 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
if (node == NULL) return 0;
  1. 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度
int leftdepth = getdepth(node->left);       // 左
int rightdepth = getdepth(node->right);     // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;

前序遍历(中左右)

class solution {
public:
    int result;
    void getdepth(treenode* node, int depth) {
        result = depth > result ? depth : result; // 中

        if (node->left == NULL && node->right == NULL) return ;

        if (node->left) { // 左
            depth++;    // 深度+1
            getdepth(node->left, depth);
            depth--;    // 回溯,深度-1
        }
        if (node->right) { // 右
            depth++;    // 深度+1
            getdepth(node->right, depth);
            depth--;    // 回溯,深度-1
        }
        return ;
    }
    int maxdepth(treenode* root) {
        result = 0;
        if (root == NULL) return result;
        getdepth(root, 1);
        return result;
    }
};

559.n叉树的最大深度

前序遍历

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

后序遍历

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

111.二叉树的最小深度

文章讲解:代码随想录 (programmercarl.com)

题目链接:111. 二叉树的最小深度 - 力扣(LeetCode)

视频讲解:看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度

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

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

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

后序遍历

比较左右子树的高度

最小深度:左右子树最小的高度 + 1【注意:若没有左/右子树,不能0 + 1】

class Solution {
public:
    int getMin (TreeNode* cur) {
        if (cur == NULL) return 0;
        int leftDepth = getMin(cur->left);
        int rightDepth = getMin(cur->right);

        if (cur->left && !cur->right) return leftDepth + 1;
        if (!cur->left && cur->right) return rightDepth + 1;
        int result = 1 + min(leftDepth, rightDepth);
        return result;
    }
    int minDepth(TreeNode* root) {
        return getMin(root);
    }
};

前序遍历

计算每一层的深度

最小深度:比较每个叶子节点的深度

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

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

文章讲解:代码随想录 (programmercarl.com)

视频讲解:要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量

题目链接:222. 完全二叉树的节点个数 - 力扣(LeetCode)

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

class Solution {
public:
    int cout (TreeNode* cur) {
        int leftDepth = 0;
        int rightDepth = 0;
        TreeNode* left = cur->left;
        TreeNode* right = cur->right;
        while (left) {
            leftDepth++;
            left = left->left;
        }
        while (right) {
            rightDepth++;
            right = right->right;
        }
        if (leftDepth == rightDepth) return (2 << leftDepth) - 1;
        int leftCount = 0;
        int rightCount = 0;
        if (cur->left) leftCount = cout(cur->left);
        if (cur->right) rightCount = cout(cur->right);
        int count = leftCount + rightCount + 1;
        return count;
    }
    int countNodes(TreeNode* root) {
       if (root == NULL) return 0;
       return cout(root);
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

囿丫七

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值