二叉树的最大深度
递归法
本题可以使用前序(中左右),也可以使用后序遍历(左右中),使用前序求的就是深度,使用后序求的是高度。
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度是从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数或者节点数(取决于高度从0开始还是从1开始)
根节点的高度就是二叉树的最大深度,本题可以通过后序求的根节点高度来求的二叉树最大深度。
用后序遍历(左右中)来计算树的高度。
1.确定递归函数的参数和返回值:参数就是传入树的根节点,返回就返回这棵树的深度,所以返回值为int类型。
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;
//完整C++代码
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(TerrNode* root){
return getdepth(root);
}
};
代码精简后:
class Solution{
public:
int maxDepth(TreeNode* root){
if(root == null)return 0;
return 1 + max(maxDepth(root->left),maxDepth(root->right));
}
};
//使用前序遍历,深度回溯
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--;
}
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;
}
};
迭代法
使用迭代法的话,使用层序遍历最为合适,因为最大的深度就是二叉树的层数,和层序遍历的方式极其吻合。
在二叉树中,一层一层的来遍历二叉树,记录一下遍历的层数就是二叉树的深度。可以使用二叉树层序遍历的模板来解:
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;
}
};
n叉树的最大深度
给定一个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;
}
};
迭代法
//层序遍历
class Solution{
public:
int maxDepth(Node* root){
queue<Node*>que;
if(root != NULL)que.push(root);
int depth = 0;
while(!que.empty()){
in size =que.size();
depth++;//记录深度
for(int i = 0;i < size;i++){
Node* node = que.front();
que.pop();
for(int j = 0;j < node->children.size();j++)
{
if(node->children[j])que.push(node->children[j]);
}
}
}
return depth;
}
};
二叉树的最小深度
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。
叶子节点是指没有子节点的节点。
这个题依然是前序遍历和后序遍历都可以,前序遍历求的是深度,后序遍历求高度。
二叉树节点的深度:指从根节点到该节点的最长简单路径边的条数或者节点数(取决于深度从0开始还是从1开始)
二叉树节点的高度:指从该节点到叶子节点的最长简单路径边的条数后者节点数(取决于高度从0开始还是从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;
遍历的顺序为后序(左右中),可以看出:求二叉树的最小深度和求二叉树的最大深度的差别主要在于处理左右孩子不为空的逻辑。
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;
if(root->left == NULL && root->right != NULL){
return 1 + minDepth(root->right);
}
if(root->left != NULL && root->right == NULL){
return 1 + minDepth(root->left);
}
return 1 + min(minDepth(root->left),minDepth(root->right));
}
};
前序遍历的方式
class Solution{
private:
int result;
void getdepth(TreeNode* node,int depth){
//函数递归终止条件
if(node == nullptr){
return;
}
//中,处理逻辑:判断是不是叶子节点
if(node->left == nullptr && node->right == nullptr){
result = min(result,depth);
}
if(node->left){
getdepth(node->left,depth + 1);
}
if(node->right){
getdepyh(node->right,depth + 1);
}
return ;
}
public:
int minDepth(TreeNode* root){
if(root == nullptr){
return 0;
}
result = INT_MAX;
getdepth(root,1);
returnresult;
}
};
迭代法
使用层序遍历
注意点:只有当左右孩子都为空的时候,才说明遍历到最低点了。如果其中一个孩子不为空,则不是最低点。
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++){
TreNode* 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;
}
};
完全二叉树的节点个数
普通二叉树
这道题的递归法和求二叉树的深度写法类似,而迭代法,层序遍历的遍历模板稍稍修改一下,记录遍历的节点数量就可以了。
递归遍历的顺序依然是后序(左右中)
递归
求二叉树深度:
1.确定递归函数的参数和返回值:参数就是传入的根节点,返回就返回以该节点为根节点二叉树的节点数量,所以返回值为int类型。
int getNodeNum(TreeNode* cur){
2.确定终止条件:如果为空节点的话,就返回0,表示节点数为0.
if(cur == NULL)return 0;
3.确定单层递归的逻辑:先求它的左子树的节点数量,再求右子树的节点数量,最后取总和再加一(加1是因为算上当前中间节点)就是目前节点为根节点的节点数量
int leftNum = getNodeNum(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:
in countNodes(TreeNode* root){
return getNodesNum(root);
}
};
//精简后的代码
class Solution{
public:
int countNodes(TreeNode* root){
if(root == NULL)return 0;
return 1 + countNodes(root->left) + countNodes(root->right);
}
};
迭代
二叉树层序遍历
用层序遍历的模板,稍做改动,加一个变量result,统计节点数量就可以了。
class Solution{
public:
int countNodes(TreeNode* root){
queue<TreeNode*>que;
if(root != NULL)que.push(root);
int result = 0;
while(!que.empty()){
int size = que.size();
for(int i = 0;i < size; i++){
TreeNode* node = que.front();
que.pop();
result++;//记录节点数
if(node->left)que.push(node->left);
if (node->right)que.push(node->right);
}
}
return result;
}
};