1. 二叉树的最大深度
代码随想录: 原文
力扣题目:104.二叉树的最大深度
1.1 思路
1.1.1 递归法
- 本题可以使用前序(中左右),求的是深度
- 可以使用后序遍历(左右中),求的是高度
二叉树节点的深度: 指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度: 指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)
根节点的高度就是二叉树的最大深度
递归三部曲:
1. 确定递归函数的参数和返回值:
- 参数就是传入树的根节点
- 返回就返回这棵树的深度
int getdepth(treenode* node)
2. 确定终止条件:
- 如果为空节点的话,就返回0,表示高度为0
if (node == NULL) return 0;
3. 确定单层递归的逻辑:
- 先求它的左子树的深度
- 再求右子树的深度
- 最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度
int leftdepth = getdepth(node->left); // 左
int rightdepth = getdepth(node->right); // 右
int depth = 1 + max(leftdepth, rightdepth); // 中
return depth;
1.1.2 迭代法
- 使用迭代法的话,使用层序遍历是最为合适的
- 最大的深度就是二叉树的层数
- 一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度
1.2 代码实现
1.2.1 递归法
class solution {
public:
int getdepth(treenode* node) {
if (node == NULL) 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);
}
};
1.2.2 迭代法
class solution {
public:
int maxdepth(treenode* root) {
if (root == NULL) return 0;
int depth = 0;
queue<treenode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++; // 记录深度
for (int i = 0; i < size; i++) {
treenode* node = que.front();
que.pop();
if (node->left) que.push(node->left);
if (node->right) que.push(node->right);
}
}
return depth;
}
};
1.3 补充:n叉树的最大深度
力扣题目:559.n叉树的最大深度
思路是和二叉树思路一样的
递归法
class solution {
public:
int maxdepth(node* root) {
if (root == 0) return 0;
int depth = 0;
for (int i = 0; i < root->children.size(); i++) {
depth = max (depth, maxdepth(root->children[i]));
}
return depth + 1;
}
};
2. 二叉树的最小深度
代码随想录: 原文
力扣题目:111.二叉树的最小深度
2.1 思路
- 二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
- 二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)
- 使用后序遍历,根节点到叶子节点的最小距离,就是求高度的过程,同样是最小深度
2.1.1 递归法
递归三部曲
1. 确定递归函数的参数和返回值
- 参数为要传入的二叉树根节点
- 返回的是int类型的深度
int getDepth(TreeNode* node)
2. 确定终止条件
- 遇到空节点返回0,表示当前节点的高度为0
if (node == NULL) return 0;
3. 确定单层递归的逻辑
- 遍历的顺序为后序(左右中)
- 如果左子树为空,右子树不为空,说明最小深度是 1 + 右子树的深度
- 右子树为空,左子树不为空,最小深度是 1 + 左子树的深度
- 如果左右子树都不为空,返回左右子树深度最小值 + 1
- 求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑
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;
2.1.2 迭代法
- 使用层序遍历的方式来解决
- 只有当左右孩子都为空的时候,才说明遍历到最低点了
- 如果其中一个孩子不为空则不是最低点
2.2 代码实现
递归法
class Solution {
public:
int getDepth(TreeNode* node) {
if (node == NULL) return 0;
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;
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
迭代法
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
int depth = 0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()) {
int size = que.size();
depth++; // 记录最小深度
for (int i = 0; i < size; i++) {
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;
}
};
3. 完全二叉树的节点个数
代码随想录: 原文
力扣题目:222.完全二叉树的节点个数
3.1 思路
3.1.1 普通二叉树
1. 确定递归函数的参数和返回值
- 参数就是传入树的根节点
- 返回以该节点为根节点二叉树的节点数量
- 返回值为int类型
int getNodesNum(TreeNode* cur) {
2. 确定终止条件:
如果为空节点的话,就返回0,表示节点数为0
if (cur == NULL) return 0;
3. 确定单层递归的逻辑:
- 求它的左子树的节点数量
- 求右子树的节点数量
- 总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
return treeNum;
3.1.2 完全二叉树
在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^(h-1) 个节点。
- 情况一:满二叉树,直接用 2^树深度 - 1 来计算,注意这里根节点深度为1
- 情况二:最后一层叶子节点没有满,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树
1. 确定递归函数的参数和返回值
- 参数就是传入树的根节点
- 返回以该节点为根节点二叉树的节点数量
- 返回值为int类型
int getNodesNum(TreeNode* cur) {
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,返回满足满二叉树的子树节点数量
}
3. 单层递归的逻辑
后序遍历
int leftTreeNum = countNodes(root->left); // 左
int rightTreeNum = countNodes(root->right); // 右
int result = leftTreeNum + rightTreeNum + 1; // 中
return result;
3.2 代码实现
普通二叉树
class Solution {
private:
int getNodesNum(TreeNode* cur) {
if (cur == NULL) return 0;
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
return treeNum;
}
public:
int countNodes(TreeNode* root) {
return getNodesNum(root);
}
};
完全二叉树
class Solution {
public:
int countNodes(TreeNode* root) {
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,所以leftDepth初始为0
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
};
- 时间复杂度:
- 空间复杂度:
4. 总结
后序遍历会持续递归到最下面的一棵子树,写代码的时候不要看整棵树,按照最下面那颗子树的结构来写就行
学习时间:130min
评论区