二叉树的最大深度
如果使用后序遍历
其实是求的根节点的高度,但是根节点的高度就是二叉树的最大深度。
/**
* 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:
// 1.确定参数:参数只需要指针
int maxDepth(TreeNode* root) {
// 2.判断递归终止条件,遇到空指针就返回
if(root == nullptr) return 0;
// 3.确定递归逻辑,使用后续遍历访问顺序,获取左子树和右子树中的最大高度,取最大值
int left = maxDepth(root->left);
int right = maxDepth(root->right);
return max(left,right) + 1;
}
};
如果使用前序遍历,那么求的就是深度,因为是从根节点开始统计深度的,而不是从叶节点开始。
/**
* 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 res = 0;
// 前序遍历
void getDepth(TreeNode* cur, int depth) {
res = max(res, depth);
if (cur->left == nullptr && cur->right == nullptr) {
return;
}
if (cur->left)
getDepth(cur->left, depth + 1);
if (cur->right)
getDepth(cur->right, depth + 1);
return;
}
// 1.参数只需要指针
int maxDepth(TreeNode* root) {
if (root == nullptr)
return 0;
getDepth(root, 1);
return res;
}
};
二叉树的最小深度
迭代法上一篇文章里有,这里使用递归法
当遍历到空节点的时候返回0,但是需要注意,如果一个节点并非叶子节点,但是其左孩子或右孩子为空,就可能返回错误的高度,需要对于两种情况进行判断。
/**
* 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 getDepth(TreeNode* cur){
if(cur == nullptr) return 0;
int left = getDepth(cur->left);
int right = getDepth(cur->right);
if(cur->left == nullptr && cur->right != nullptr) return 1 + right;
if(cur->left != nullptr && cur->right == nullptr) return 1 + left;
return 1 + min(left,right);
}
int minDepth(TreeNode* root) {
return getDepth(root);
}
};
可以思考另一种方式,当节点不是叶子节点的时候需要特判,那么我们直接对于叶子节点返回,就可以不写判断的代码。但是需要判断其左右孩子是否存在(因为不是遍历到空节点的时候就能返回正确的答案)
/**
* 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 getDepth(TreeNode* cur) {
if (cur->left == nullptr && cur->right == nullptr)
return 1;
int left = INT_MAX;
int right = INT_MAX;
if (cur->left)
left = getDepth(cur->left);
if (cur->right)
right = getDepth(cur->right);
return 1 + min(left, right);
}
int minDepth(TreeNode* root) {
if (root == nullptr)
return 0;
return getDepth(root);
}
};
完全二叉树的节点数量
层序遍历求个数
/**
* 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 == nullptr)
return 0;
queue<TreeNode*> q;
int num = 0;
q.push(root);
while (!q.empty()) {
TreeNode* cur = q.front();
q.pop();
num++;
if (cur->left)
q.push(cur->left);
if (cur->right)
q.push(cur->right);
}
return num;
}
};
后序遍历求个数,先求左子树的节点数,再求右子树的节点数,最后返回1+左+右
/**
* 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 == nullptr)
return 0;
int left = countNodes(root->left);
int right = countNodes(root->right);
return 1 + left + right;
}
};