代码随想录算法训练营第十六天|● 104.二叉树的最大深度 559.n叉树的最大深度 ● 111.二叉树的最小深度 ● 222.完全二叉树的节点个数

今天和明天要准备面试,暂时先停止两天,会在后续两天补上

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 getheight(TreeNode* node) {
        if (node == nullptr) return 0;
        int leftheight = getheight(node->left); // 左
        int rightHeight = getheight(node->right); // 右
        int height = 1 + max(leftheight, rightHeight);
        return height;
    }
    int maxDepth(TreeNode* root) {
        return getheight(root);
    }
};

总结:本题首先要明确什么是高度,什么是深度,对于不同的理解可以有不同的解法,所以一定要明白

简化写法

class Solution {
public:
    int getheight(TreeNode* node) {
        if (node == nullptr) return 0;
        return 1 + max(getheight(node->left), getheight(node->right));
    }
    
    int maxDepth(TreeNode* root) {
        return getheight(root);
    }
};

这种写法不容易理解,可以在二刷的时候来看,现在的能力不够,还需要努力。

思路2迭代法
这个写法在层序遍历的时候写过,只需要加一个深度变量来统计深度即可

class Solution {
public:
    int maxDepth(TreeNode* root) {
        queue<TreeNode*> que;
        if (root != nullptr) {
            que.push(root);
        }
        int count = 0;
        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left) que.push(node->left);
                if (node->right) que.push(node->right);
            }
            count++;
        }
        return count;
    }
};

559.n叉树的最大深度

题目链接

给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
在这里插入图片描述
思路1:迭代法

  1. 和遍历二叉树一样,不过在循环里面插入节点的时候,使用了auto child : node->children,可能以后遇到了会忘记,希望自己牢记
class Solution {
public:

    int maxDepth(Node* root) {
        if (root == nullptr) return 0;
        queue<Node*> que;
        que.push(root);
        int depth = 0;

        while (!que.empty()) {
            int size = que.size();
            
            while (size--) {
                Node* node = que.front();
                que.pop();
                for (auto child : node->children) {
                    que.push(child);
                }
            }
            depth++;
        }
        return depth;
    }
};

思路2: 递归

  1. 和二叉树的最大深度一样,这种递归不是很好理解,还需要加强练习。
class Solution {
public:

    int maxDepth(Node* root) {
        if (root == nullptr) return 0;
        int depth = 0;
        for (int i = 0; i < root->children.size(); i++) {
            depth = max(depth, maxDepth(root->children[i]));
        }
        return depth + 1;
    }
};

111.二叉树的最小深度

题目链接
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。

思路

  1. 首先,还是要强调:本题依然是前序遍历和后序遍历都可以,前序求的是深度,后序求的是高度。

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

  2. 题目中说的是:最小深度是从根节点到最近叶子节点的最短路径上的节点数量。注意是叶子节点。
    什么是叶子节点,左右孩子都为空的节点才是叶子节点!

递归法
1/确定递归函数的参数和返回值
参数为要传入的二叉树根节点,返回的是int类型的深度。

int getDepth(TreeNode* node)

2、确定函数的终止条件
终止条件也是遇到空节点返回0,表示当前节点的高度为0。

if (node == nullptr) return 0;

3、确定单层递归的逻辑
(牢记左右节点都为空才是叶子节点),所以代码如下

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;

完整代码如下

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;
        }
        if (node->left != nullptr && node->right == nullptr) {
            return 1 + leftDepth;
        }

        int minL = 1 + min(leftDepth, rightDepth); //1是为了加上根节点
        return minL;
    }

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

精简代码:不容易理解,二刷的时候再看即可

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

迭代法
在遍历的过程中加上判断叶子条件的if语句,当节点的左右孩子都为空的时候,返回深度。

class Solution {
public:
    int minDepth(TreeNode* root) {
        if (root == nullptr) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int count = 0;

        while (!que.empty()) {
            int size = que.size();
            count++;
            while (size--) {
                TreeNode* node = que.front();
                que.pop();
                if (node->left != nullptr) que.push(node->left);
                if (node->right != nullptr) que.push(node->right);
                if (node->left == nullptr && node->right == nullptr) {
                    return count;
                }
            }
        }
        return count;
    }
};

总结:对于这道题,最重要的一个条件就是 :左右孩子都为空的节点才是叶子节点!
当知道这个条件以后,对于遍历来说就很容易了。

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

题目链接

给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。

完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。

思路1:常规迭代法

  1. 遍历整个二叉树,在弹出队列元素的时候计数,输出结果
class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        queue<TreeNode*> que;
        que.push(root);
        int count = 0;

        while (!que.empty()) {
            int size = que.size();
            while (size--) {
                auto node = que.front();
                que.pop();
                count++;
                if (node->left) {
                    que.push(node->left);
                }
                
                if (node->right) {
                    que.push(node->right);
                }
            }
        }
        return count;
    }
};

总结1:虽然能AC,但是并不是本道题的主要完成的方法。接下来看递归法

思路2:完全二叉树递归法

完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。
对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。

  1. 分别向左遍历和向右遍历,因为是完全二叉树的缘故,只用遍历最外边即可。
  2. 判断左右相等的情况,当左右深度相同的情况,向他们的中间节点返回深度
    在这里插入图片描述

递归三部曲的第二部:确定终止条件如下

if (root == nullptr) return 0; 
// 开始根据左深度和右深度是否相同来判断该子树是不是满二叉树
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
while (left) {  // 求左子树深度
    left = left->left;
    leftDepth++;
}
while (right) { // 求右子树深度
    right = right->right;
    rightDepth++;
}
if (leftDepth == rightDepth) {
    return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,返回满足满二叉树的子树节点数量
}

递归第三步:

int leftTreeNum = countNodes(root->left);       // 左
int rightTreeNum = countNodes(root->right);     // 右
int result = leftTreeNum + rightTreeNum + 1;    // 中
return result;

整体代码如下:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if (root == nullptr) return 0;
        TreeNode* left = root->left, *right = root->right;
        int leftLenth = 0, rightLen = 0;

        while (left) {
            left = left->left;
            leftLenth++;
        }
        while (right) {
            right = right->right;
            rightLen++;
        }
        if (leftLenth == rightLen) {
            return (2<<leftLenth) - 1;
        }

        int leftTree = countNodes(root->left);
        int rightTree = countNodes(root->right);
        return leftTree + rightTree + 1;
    }
};

总结:还是不太喜欢递归法,在使用的过程中害怕写不好递归的逻辑,反而要多加练习才行,本题难度不大,重要的还是要学会利用完全二叉树和满二叉树的特性,这样解题思路更多。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值