一、104. 二叉树的最大深度
题目链接:力扣
文章讲解:代码随想录
视频讲解:二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度
题目:
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
说明: 叶子节点是指没有子节点的节点。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
//递归法前序遍历
/* int order(TreeNode *node, int depth)
{
if (!node) return depth;
return max(order(node->left, depth+1), order(node->right, depth+1));
}*/
//迭代法层序遍历
int maxDepth(TreeNode* root) {
// return order(root,0);
queue<TreeNode*> queue;
vector<TreeNode*> floor;
if (!root) return 0;
queue.push(root);
int depth=0;
while (!queue.empty())
{
int size = queue.size();
for(int i = 0; i < size; i++)
{
TreeNode *node = queue.front();
queue.pop();
if (node->left) queue.push(node->left);
if (node->right) queue.push(node->right);
}
depth++;
}
return depth;
}
};
时间复杂度: O(n) 空间复杂度: O(n)
⏲:2:37
总结:使用前序求的就是深度,使用后序求的是高度。深度指的是从root视角向下,高度是叶子节点向上。
二、104. 二叉树的最大深度
题目链接:力扣
文章讲解:代码随想录
视频讲解:二叉树的高度和深度有啥区别?究竟用什么遍历顺序?很多录友搞不懂 | 104.二叉树的最大深度
题目:
给定一个 N 叉树,找到其最大深度。
最大深度是指从根节点到最远叶子节点的最长路径上的节点总数。
N 叉树输入按层序遍历序列化表示,每组子节点由空值分隔(请参见示例)。
代码:
/*
// Definition for a Node.
class Node {
public:
int val;
vector<Node*> children;
Node() {}
Node(int _val) {
val = _val;
}
Node(int _val, vector<Node*> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
public:
int maxDepth(Node* root) {
if(!root) return 0;
int _max = 0;
for(int i = 0; i < root->children.size(); i++)
_max = max(_max, maxDepth(root->children[i]));
return _max+1;
}
};
时间复杂度: O(n) 空间复杂度: O(n)
⏲:2:53
总结:max最好不要作为变量名。
三、111. 二叉树的最小深度
题目链接:力扣
文章讲解:代码随想录
视频讲解:看起来好像做过,一写就错! | LeetCode:111.二叉树的最小深度
题目:
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明:叶子节点是指没有子节点的节点。
代码:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
/*递归法
if (!node) return 0;
if (!node->left && node->right) return minDepth(node->right) + 1;
if (node->left && !node->right) return minDepth(node->left) + 1;
if (!node->left && !node->right) return 1;
return min(minDepth(node->left), minDepth(node->right)) + 1;*/
if (!root) return 0;
queue<TreeNode*> queue;
queue.push(root);
int depth = 0;
while (!queue.empty())
{
int size = queue.size();
for (int i = 0; i < size; i++)
{
TreeNode *node = queue.front();
queue.pop();
if (node->left) queue.push(node->left);
if (node->right) queue.push(node->right);
if (!node->left && !node->right) return depth+1;
}
depth++;
}
return depth;
}
};
时间复杂度: O(n) 空间复杂度: O(n)
⏲:3:42
总结:本题关键在于叶子节点,例如root节点(左空右右或右空左右)不会是叶子节点,故这题在判断叶子节点的条件上下文章。
四、222. 完全二叉树的节点个数
题目链接:力扣
文章讲解:代码随想录
视频讲解:要理解普通二叉树和完全二叉树的区别! | LeetCode:222.完全二叉树节点的数量
题目:
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
代码:
法一:层序遍历+后序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
/*迭代层序遍历
if (!root) return 0;
queue<TreeNode*> queue;
queue.push(root);
int mount = 0;
while (!queue.empty())
{
int size = queue.size();
for (int i = 0;i < size; i++)
{
TreeNode *node = queue.front();
queue.pop();
if (node->left) queue.push(node->left);
if (node->right) queue.push(node->right);
}
mount += size;
}
return mount;*/
//后序遍历 递归
if (!root) return 0;
TreeNode *left = root->left;
TreeNode *right = root->right;
int leftdepth = 0, rightdepth = 0;
while (left)
{
left = left->left;
leftdepth++;
}
while (right)
{
right = right->right;
rightdepth++;
}
if (leftdepth == rightdepth) return (2 << leftdepth) - 1;
return countNodes(root->left) + countNodes(root->right) + 1;
}
};
时间复杂度: O((logn)**2) 空间复杂度: O(n)
总结:1.普通二叉树用层序遍历。
2.根据完全二叉树的特性,只有两种情况,满二叉树或则最后一层叶子节点没有满。针对情况二分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
3.时间复杂度 O(log n × log n) :求深度的时间复杂度为 O(log(n)),每一层计算一次深度,层数也是log(n),故log n × log n。
法二:二分查找 + 位运算
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
/*int countNodes(TreeNode* root) {
if(!root) return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}*/
int countNodes(TreeNode* root) {
if(!root) return 0;
int depth = 0;
TreeNode* find_depth = root;
while(find_depth->left){
find_depth = find_depth->left;
depth++;
}
int low = 1 << depth, high = (1 << (depth+1))-1;
while(low < high){
int mid = ((high-low+1) >> 1) + low;
if (exist(root, depth, mid)) low = mid;
else high = mid-1;
}
return low;
}
bool exist(TreeNode* root, int depth, int mid){
int bits = 1 << (depth - 1);
TreeNode* node = root;
while (node && bits > 0) {
if (!(bits & mid)) {
node = node->left;
} else {
node = node->right;
}
bits >>= 1;
}
return node;
}
};
时间复杂度: O((logn)**2) 空间复杂度: O(1)
⏲:46:59
总结:1.此法为先求层数,再求最后层个数。求最后层数分为二分法和位运算两个部分。
2.二分法基于最后一层的序号在于2**n到2**(n+1)-1(完全二叉树的结点序号和个数一致),且最后一层的结点从左到右依次增大(有序),可用二分法寻找最后一个结点的位置。
3.位运算基于完全二叉树亲子之间的关系(父结点序号*2与*2+1为左右子结点)故可用位运算基于二分法从上向下搜索验证。
4.此题二分法为左开右闭,目的是为了最后能停留在最后一个存在的序号上。
5.位运算实际为,二分法的mid的二进制各位的匹配。为0则向左,为1则向右。(哈夫曼编码)。