104.二叉树的最大深度
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从1开始)
而根节点的高度就是二叉树的最大深度,所以本题中我们通过后序求的根节点高度来求的二叉树最大深度。
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
使用递归求解
递归三步曲
- 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
int getdepth(treenode* node)
- 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
if (node == NULL) return 0;
- 确定单层递归的逻辑:先求它的左子树的深度,再求的右子树的深度,最后取左右深度最大的数值 再+1 (加1是因为算上当前中间节点)就是目前节点为根节点的树的深度。
int leftdepth = getdepth(node->left); // 左
int rightdepth = getdepth(node->right); // 右
int depth = max(leftdepth, rightdepth); // 中
return depth + 1;
c++实现
class Solution104 {
public:
// 确定参数和返回值
int getHeight(TreeNode *node){
// 确定递归的终止条件
if(node == nullptr)
return 0;
// 确定单层的逻辑
int leftHeight = getHeight(node->left);
int rightHeight = getHeight(node->right);
int height = max(leftHeight, rightHeight);
return height + 1;
}
int maxDepth(TreeNode* root) {
return getHeight(root);
}
};
Python实现
class Solution104:
def getHeight(self, node: Optional[TreeNode]):
if not node:
return 0
leftHeight = self.getHeight(node.left)
rightHeight = self.getHeight(node.right)
height = max(leftHeight, rightHeight) + 1
return height
def maxDepth(self, root: Optional[TreeNode]) -> Optional[int]:
return self.getHeight(root)
559.n叉树的最大深度
这道题与104相似,关键是想清楚单层逻辑
c++实现
class Solution {
public:
// 确定参数和返回值
int getHeight(Node *node){
// 确定递归的终止条件
if(node == nullptr)
return 0;
// 确定单层的逻辑
int height = 0;
vector<Node *> childrenNode = node->children;
// 遍历本层孩子结点
for(auto it : childrenNode)
{
int childDepth = getHeight(it);
height = max(height, childDepth);
}
return height + 1;
}
int maxDepth(Node* root) {
return getHeight(root);
}
};
111.二叉树的最小深度
题目要求:最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
Note: 叶子节点是指没有子节点的节点。
遍历方式:后序遍历
那么使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,这不过这个最小距离 也同样是最小深度。
本题误区:
递归三步曲
- 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
int getdepth(treenode* node)
- 确定终止条件:如果为空节点的话,就返回0,表示高度为0。
if (node == NULL) return 0;
- 确定单层递归的逻辑:
错代码(类比最大深度)
int leftDepth = getDepth(node->left);
int rightDepth = getDepth(node->right);
int result = 1 + min(leftDepth, rightDepth);
return result;
这个代码就犯了上图中的误区
如果这么求的话,没有左孩子的分支会算为最短深度。
所以,如果左子树为空,右子树不为空,说明最小深度是 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;
c++实现
class Solution {
public:
// 确定返回值和参数
int getHeight(TreeNode *node){
// 确定终止条件
if(node == nullptr)
return 0;
// 确定单层的逻辑
int leftHeight = getHeight(node->left);
int rightHeight = getHeight(node->right);
// 叶子节点,左右孩子都为空的节点才是叶子节点
if(node->left == nullptr && node->right != nullptr)
return 1 + rightHeight;
else if (node->left != nullptr && node->right == nullptr)
return 1 + leftHeight;
int result = min(leftHeight, rightHeight);
return result + 1;
}
int minDepth(TreeNode* root) {
return getHeight(root);
}
};
Python实现
class Solution111:
def getHeight(self, node: Optional[TreeNode]):
if not node:
return 0
leftHeight = self.getHeight(node.left)
rightHeight = self.getHeight(node.right)
if node.left is None and node.right is not None:
return 1 + rightHeight
if node.left is not None and node.right is None:
return 1 + leftHeight
result = min(leftHeight, rightHeight)
return result + 1
def maxDepth(self, root: Optional[TreeNode]) -> Optional[int]:
return self.getHeight(root)
222.完全二叉树的节点个数
方法一:使用递归法作为一个普通二叉树处理
递归三步曲
- 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
int getNodesNum(TreeNode* cur)
- 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
if (cur == NULL) return 0;
- 确定单层递归的逻辑:先求它的左子树的节点数量,再求的右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
return treeNum;
c++实现
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);
}
};
方法二:使用满二叉树的性质
在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第
n
n
n 层,则该层包含
1
至
2
n
−
1
1至2^{n-1}
1至2n−1个节点。
完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。
对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。
对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
完全二叉树如图:
可以看出如果整个树不是满二叉树,就递归其左右孩子,直到遇到满二叉树为止,用公式计算这个子树(满二叉树)的节点数量
如果去判断一个左子树或者右子树是不是满二叉树呢?
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图:
在完全二叉树中,如果递归向左遍历的深度不等于递归向右遍历的深度,则说明不是满二叉树,如图:
递归三步曲
- 确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
int getNodesNum(TreeNode* cur)
- 确定终止条件:如果为空节点的话,就返回0,表示节点数为0。
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,返回满足满二叉树的子树节点数量
}
- 确定单层递归的逻辑:先求它的左子树的节点数量,再求的右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量。
int leftNum = getNodesNum(cur->left); // 左
int rightNum = getNodesNum(cur->right); // 右
int treeNum = leftNum + rightNum + 1; // 中
return treeNum;
c++实现
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;
}
};