题目:104.二叉树的最大深度
文章链接:代码随想录
题目链接:力扣题目链接
难易点:
深度指从根节点到该节点的最长简单路径边的条数或者节点数(从上往下)
高度指指从该节点到叶子节点的最长简单路径边的条数或者节点数(从下往上)
解法1:递归方法
class Solution {
public:
// 确定输入输出的参数
int getheight(TreeNode* root){
// 确定终止条件
if(root == NULL) return 0; // 返回当前节点的深度为0
// 层逻辑
int leftheight = getheight(root->left); // 左节点的深度
int rightheight = getheight(root->right); // 右节点的深度
// 返回给父节点
return 1+max(leftheight,rightheight); // 中
}
int maxDepth(TreeNode* root) {
return getheight(root);
}
};
解法2:遍历法(之前遍历的时候有刷过,用队列的方式)
class Solution {
public:
int maxDepth(TreeNode* root) {
// 队列
queue<TreeNode*> que;
int result = 0; // 记录深度
if(root != NULL) que.push(root);
while(!que.empty()){
int size = que.size();
result ++;
for(int i=0; i<size; i++){
TreeNode* node = que.front(); // [2,3]
que.pop();
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return result;
}
};
题目:509.N叉树的最小深度
文章链接:代码随想录
题目链接:力扣题目链接
解法1:递归法
class Solution {
public:
// 获取n叉树的深度
int getdepth(Node* node) {
if (node == NULL) return 0;
int maxdepth = 1; //根节点也算一个深度
int size = node->children.size();
// 后序
for(int i=0; i<size ; i++){
// 获取每条支路的深度
int depth = getdepth(node->children[i])+1;
maxdepth = maxdepth > depth ? maxdepth:depth;
}
return maxdepth;
}
int maxDepth(Node* root) {
return getdepth(root);
}
};
解法2:层序遍历
class Solution {
public:
// 层序遍历获取二叉树的深度
int maxDepth(Node* root) {
int result = 0;
if (root == NULL) return result;
queue<Node*>que;
que.push(root);
while (!que.empty()) {
int size = que.size();
result++;
while (size--) {
Node* cur = que.front();
que.pop();
for (int i = 0; i < cur->children.size(); i++) {
que.push(cur->children[i]);
}
}
}
return result;
}
};
题目:111.二叉树的最小深度
文章链接:代码随想录
题目链接:力扣题目链接
难易点:
最小深度是从根节点到最近叶子节点的最短路径上的节点数量。,注意是叶子节点。
左右孩子都为空的节点才是叶子节点。
解法1:递归迭代法
class Solution {
public:
// 确定输入输出参数
int getheight(TreeNode* root){
// 确定终止条件
if(root == NULL) return 0;
// 后序遍历
int leftheight = getheight(root->left);
int rightheight = getheight(root->right);
// 直接 1+min(leftheight,rightheight) 遇到单个节点的直接返回1+0
//左为空右不为空
if(root->left == NULL && root->right != NULL) return 1+rightheight;
//右为空左不为空
else if(root->left != NULL && root->right == NULL) return 1+leftheight;
// 左右都不为空,以及都为空
int result = 1+min(rightheight,leftheight);
return result;
}
int minDepth(TreeNode* root) {
return getheight(root);
}
};
class Solution {
public:
int getminDepth(TreeNode* root, int depth) {
// 遍历到叶子节点返回
if (root->left == NULL && root->right == NULL) return depth;
// 后序
// 因为到叶子节点才返回,所以单个节点的时候,需要进行判断
int leftdepth = INT_MAX;
int rightdepth= INT_MAX;
if (root->left != NULL) {
leftdepth = getminDepth(root->left, depth+1);
}
if (root->right != NULL) {
rightdepth = getminDepth(root->right, depth + 1);
}
return leftdepth < rightdepth ? leftdepth : rightdepth;
}
int minDepth(TreeNode* root) {
if(root==NULL) return 0;
return getminDepth(root,1);
}
};
解法2:迭代遍历
class Solution {
public:
int minDepth(TreeNode* root) {
if(root == NULL) return 0;
// 队列
queue<TreeNode*> que;
int result = 0; // 记录深度
que.push(root);
while(!que.empty()){
int size = que.size();
result ++;
for(int i=0; i<size; i++){
TreeNode* node = que.front(); // [9,20]
que.pop();
//当遍历到叶子节点则进行返回
if(!node->left && !node->right) return result;
if(node->left) que.push(node->left);
if(node->right) que.push(node->right);
}
}
return result;
}
};
题目:222.完全二叉树的节点个数
文章链接:代码随想录
题目链接:力扣题目链接
解法1:递归
class Solution {
public:
int getNume(TreeNode* node){
// 终止条件
if(node == NULL) return 0;
// 后序遍历
int leftnume = getNume(node->left);
int rightnume = getNume(node->right);
int result = leftnume + rightnume + 1 ; // 记得加上本身的节点
return result;
}
int countNodes(TreeNode* root) {
return getNume(root);
}
};
解法2:层序遍历
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;
}
};
解法3:完全二叉树的特性
class Solution {
public:
int countNodes(TreeNode* root) {
// 确定终止条件
if(root == NULL) 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; // 2<<1相当于2^2
}
// 如果不满足则向下递归,不能直接传left,right
return countNodes(root->left) + countNodes(root->right)+ 1; //注意加上自身
}
};
总结: