题目链接
给定一个二叉树,找出其最大深度。
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
深度是任意节点到根节点的距离,根节点的深度是1。
高度是任意节点到叶子结点的距离,叶子结点的高度是1。
要求高度应该采用后序遍历,左右中,把子节点的高度返回给父节点,则父节点的高度就是子节点加一。
要求深度应该采用前序遍历,中左右,把父节点的高度返回给子节点,则子节点的深度就是父节点加一。
解题思路就是根节点的高度就是二叉树的最大深度。
递归三步走
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr) return 0;
int leftdepth=maxDepth(root->left);
int rightdepth=maxDepth(root->right);
int depth=1+max(leftdepth,rightdepth);
return depth;
}
};
还有一种更精简的写法:
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr) return 0;
return 1+max(maxDepth(root->left),maxDepth(root->right));
}
};
但是这种不太好理解,也不太好体现出来后序遍历的思路。
相关的有一个n叉树的最大深度
递归法代码:
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=1+max(depth,maxDepth(root->children[i]));
//是不对的,一层的子节点加一次一就可以了
depth=max(depth,maxDepth(root->children[i]));
}
return depth+1;
}
};
层序遍历法可以看层序遍历,有LeetCode上使用层序遍历解的十道题。
使用迭代法的话,使用层序遍历是最为合适的,因为最大的深度就是二叉树的层数,记录一下遍历的层数就是二叉树的深度。
class Solution {
public:
int maxDepth(TreeNode* root) {
queue<TreeNode*> que;
int depth=0;
if(root) que.push(root);
while(!que.empty()){
int size=que.size();
while(size--){
TreeNode* cur=que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
depth++;
}
return depth;
}
};
这里可以看层序遍历,有LeetCode上使用层序遍历解的十道题。
所有节点遍历一遍,复杂度O(n)
空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(logN)。
求高度是从下往上数的,收集左右孩子信息,向上返回的这类思路都需要用到后序遍历,所以求高度用后序遍历(左右中)
本题的递归法也可以用前序遍历来写,但是会复杂很多,有回溯,
class solution {
public:
int result;
void getdepth(treenode* node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ;
if (node->left) { // 左
depth++; // 深度+1
getdepth(node->left, depth);
depth--; // 回溯,深度-1
}
if (node->right) { // 右
depth++; // 深度+1
getdepth(node->right, depth);
depth--; // 回溯,深度-1
}
return ;
}
int maxdepth(treenode* root) {
result = 0;
if (root == NULL) return result;
getdepth(root, 1);
return result;
}
};
# 简化后如下
class solution {
public:
int result;
void getdepth(treenode* node, int depth) {
result = depth > result ? depth : result; // 中
if (node->left == NULL && node->right == NULL) return ;
if (node->left) { // 左
getdepth(node->left, depth + 1);
}
if (node->right) { // 右
getdepth(node->right, depth + 1);
}
return ;
}
int maxdepth(treenode* root) {
result = 0;
if (root == 0) return result;
getdepth(root, 1);
return result;
}
};
本题学习时间⌛️:80min
题目链接
给定一个二叉树,找出其最小深度。
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
说明: 叶子节点是指没有子节点的节点。
使用后序遍历,其实求的是根节点到叶子节点的最小距离,就是求高度的过程,不过这个最小距离 也同样是最小深度。
本题还有一个误区,最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
所以:
所以下面代码是错的:
//错误示范
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==nullptr) return 0;
int leftdepth=minDepth(root->left);
int rightdepth=minDepth(root->right);
int depth=1+min(leftdepth,rightdepth);
return depth;
}
};
应该加一个判断条件:
class Solution {
public:
int minDepth(TreeNode* root) {
if(root==nullptr) return 0;
int depth=0;
int leftdepth=minDepth(root->left);
int rightdepth=minDepth(root->right);
if(root->left==nullptr&&root->right!=nullptr){
return 1+rightdepth;
} //左
if(root->left!=nullptr&&root->right==nullptr){
return 1+leftdepth;
} //右
depth=1+min(leftdepth,rightdepth);//中
return depth;
}
};
所有节点遍历一遍,复杂度O(n)
空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(logN)。
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int depth=0;
if(root) que.push(root);
while(!que.empty()){
int size=que.size();
depth++;
while(size--){
TreeNode* cur=que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
if(!cur->left&&!cur->right) return depth;
}
}
return depth;
}
};
这里可以看层序遍历,有LeetCode上使用层序遍历解的十道题。
本题的递归法也可以用前序遍历来写,但是会复杂很多,有回溯
class Solution {
private:
int result;
void getdepth(TreeNode* node, int depth) {
// 函数递归终止条件
if (root == nullptr) {
return;
}
// 中,处理逻辑:判断是不是叶子结点
if (root -> left == nullptr && root->right == nullptr) {
res = min(res, depth);
}
if (node->left) { // 左
getdepth(node->left, depth + 1);
}
if (node->right) { // 右
getdepth(node->right, depth + 1);
}
return ;
}
public:
int minDepth(TreeNode* root) {
if (root == nullptr) {
return 0;
}
result = INT_MAX;
getdepth(root, 1);
return result;
}
};
本题学习时间⌛️:1.3h
题目链接
给出一个完全二叉树,求出该树的节点个数。
示例 1:
输入:root = [1,2,3,4,5,6]
输出:6
示例 2:
输入:root = []
输出:0
示例 3:
输入:root = [1]
输出:1
确定递归函数的参数和返回值,终止条件这里就不说了,
单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一 (加1是因为算上当前中间节点)就是目前节点为根节点的节点数量
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
int leftcount=countNodes(root->left);
int rightcount=countNodes(root->right);
int count=leftcount+rightcount+1;
return count;
}
};
所有节点遍历一遍,复杂度O(n)
空间复杂度主要取决于递归时栈空间的开销,最坏情况下,树呈现链状,空间复杂度为 O(N)。平均情况下树的高度与节点数的对数正相关,空间复杂度为O(logN)。
加一个变量count,统计节点数量就可以了
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> que;
if(root) que.push(root);
int count=0;
while(!que.empty()){
int size=que.size();
while(size--){
TreeNode* cur=que.front();
que.pop();
count++;
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return count;
}
};
时间复杂度:O(n)
空间复杂度:O(n)
刚才的两种解法都没有用到完全二叉树的特性,
在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。
如何利用二叉树的特性
完全二叉树只有两种情况,情况一:就是满二叉树,情况二:最后一层叶子节点没有满。
对于情况一,可以直接用 2^树深度 - 1 来计算,注意这里根节点深度为1。
对于情况二,分别递归左孩子,和右孩子,递归到某一深度一定会有左孩子或者右孩子为满二叉树,然后依然可以按照情况1来计算。
如何去判断一个左子树或者右子树是不是满二叉树呢?
在完全二叉树中,如果递归向左遍历的深度等于递归向右遍历的深度,那说明就是满二叉树。如图:
左右深度不相等就不是满二叉树
这种根本就不是完全二叉树
整个递归的思路是:
(1)求当前树的左深度和右深度
(2)如果相等,是满二叉树,用公式返回节点数量
(3)如果不相等,就再求其左子树和右子树的节点数量,当前树的节点数量=左子树节点数量+右子树节点数量+1。由于递归到某一个深度,一定会遇到满二叉树,所以,递归到底层,一定是由公式法返回的。
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
int leftdepth=0,rightdepth=0;// 这里初始为0是有目的的,为了下面求指数方便
TreeNode* left=root->left;
TreeNode* right=root->right;
while(left){
left=left->left;
leftdepth++;
}
while(right){
right=right->right;
rightdepth++;
}
if(leftdepth==rightdepth) {
// 注意(2<<1) 相当于2^2,所以leftDepth初始为0
return (2<<leftdepth)-1;
}
return countNodes(root->left)+countNodes(root->right)+1;
}
};
计算左子树和右子树深度的 while 循环的时间复杂度是 O(log n),其中 n 是节点的数量。在完全二叉树中,左子树或右子树的深度最多为 log n。
递归调用的时间复杂度也是 O(log n)。在完全二叉树中,每一层的节点数都是固定的,因此递归的深度也是 log n。
每次计算满二叉树的时候,计算的其实就是当前树高,平均情况下即 O(logn),因为这里是完全二叉树,不考虑最坏情况下的链式的树高为O(N)的树,每次递归调用的都是下一层的子树,总共调用了“树的高度”次,即 O(logn),所以时间复杂度为 O(logn) * O(logn);
此外,使用了递归,额外调用了栈空间,最多压栈”当前树高“个元素,所以空间复杂度为 O(logn)。
位运算<<符号用来将一个数的各二进制位全部左移n位,左移一位就是乘2。
利用完全二叉树的性质来计算是没想到的,其中一个核心就是完全二叉树的左右节点的终止一定满足满二叉树
本题学习时间:90miin