基础知识
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数。
二叉树节点的高度:从该节点到叶子节点的最长简单路径边的条数或者节点数。
根节点的高度就是二叉树的最大深度,因此可以通过后序求根节点高度来求二次v蛤属的最大深度。
最小深度:根节点到叶子节点的最小距离,也是求高度的过程,因此可以使用后序遍历
Leetcode: 104 二叉树最大深度计算
递归法
按照后序遍历顺序,左右中,这样只需要定义一个高度,然后在返回中间节点的时候,把左右子树的高度返回+1
class Solution {
public:
int getheight(TreeNode* node){
if(node == NULL) return 0;
int leftheight = getheight(node->left);//左
int rightheight = getheight(node->right);//右
int mid = 1 + max(leftheight, rightheight);//中
return mid;
}
int maxDepth(TreeNode* root) {
return getheight(root);
}
};
迭代法,层级遍历
只需要统计总共有多少层就可以了。
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root == NULL) return 0;//需要加上这个判断,不然会报错
int deep = 0;
queue<TreeNode*> que;//定义队列
if(root != NULL) que.push(root);//先将根节点输入队列
while(!que.empty()){
int nodesize = que.size();//计算每层的大小
deep++;
while(nodesize--){
TreeNode* node = que.front();//先取当前节点,弹出队列,并将其左右子节点加入队列
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return deep;
}
};
Leetcode: 599 N叉树的最大深度计算
与上题思路一样,只是分支多了。
递归法
class Solution {
public:
int maxDepth(Node* root) {
if(root == 0) return 0;
int deep = 0;
for(Node* child: root->children){
deep = max(deep, maxDepth(child));
}
return deep + 1;
}
};
迭代法
class Solution {
public:
int maxDepth(Node* root) {
if(root == NULL) return 0;
int deep = 0;
queue<Node*> que;//定义队列
if(root != NULL) que.push(root);//先将根节点输入队列
while(!que.empty()){
int nodesize = que.size();//计算每层的大小
deep++;
while(nodesize--){
Node* node = que.front();
que.pop();
for(Node* child: node->children){
que.push(child);
}
}
}
return deep;
}
};
Leetcode: 111 二叉树最小深度计算
本题存在一个误区,就是最小深度是从根节点到最近叶子节点的最短路径上的节点数量。注意是叶子节点!
递归法
class Solution {
public:
int getheight(TreeNode* node){
if(node == NULL) return 0;
int leftheight = getheight(node->left);
int rightheight = getheight(node->right);
if(node->left == NULL && node->right != NULL) return 1 + rightheight;
else if(node->left != NULL && node->right == NULL) return 1+leftheight;//如果存在不等于NULL的说明还没有到叶子节点
else return 1 + min(leftheight, rightheight);//包含两种情况,如果是叶子节点的话,就是1+0.
}
int minDepth(TreeNode* root) {
return getheight(root);
}
};
迭代法
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0;//需要加上这个判断,不然会报错
int deep = 0;
queue<TreeNode*> que;//定义队列
if(root != NULL) que.push(root);//先将根节点输入队列
while(!que.empty()){
int nodesize = que.size();//计算每层的大小
deep++;
while(nodesize--){
TreeNode* node = que.front();//先取当前节点,弹出队列,并将其左右子节点加入队列
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
if(node->left == NULL && node->right == NULL) return deep;//左右节点都指向NULL,说明到了
}
}
return deep;
}
};
Leetcode:222 完全二叉树节点个数
普通二叉树的节点数量
无论用那种遍历方式,只要统计一下数量就可以了。实际上完全二叉树可以直接这样计算,但是本题可能希望我们使用完全二叉树的性质来简化算法。
时间复杂度O(N)
空间复杂度O(logn)
递归法
class Solution {
public:
int getnum(TreeNode* node){
if(node == NULL) return 0;
int left = getnum(node->left);
int right = getnum(node->right);
return 1 + left + right;
}
int countNodes(TreeNode* root) {
return getnum(root);
}
};
迭代法
时间复杂度O(N)
空间复杂度O(N)
class Solution {
public:
int countNodes(TreeNode* root) {
if(root == NULL) return 0;
queue<TreeNode*> que;//定义队列
int count = 0;
if(root != NULL) que.push(root);//先将根节点输入队列
while(!que.empty()){
int nodesize = que.size();//计算每层的大小
while(nodesize--){
TreeNode* node = que.front();//先取当前节点,弹出队列,并将其左右子节点加入队列
que.pop();
count++;
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return count;
}
};
完全二叉树的节点数量
我们知道满二叉树的节点有计算公式,2^h-1,h为层数。
因此完全二叉树有两种情况
- 满二叉树,直接使用公式计算节点数量
- 最后一层叶子节点没有满,往左右节点递归直到出现满二叉树,按照第一种情况计算
递归法
代码为代码随想录
提供
时间复杂度O(logn * logn)
空间复杂度O(logn)
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;
}
};