104.二叉树的最大深度
二叉树的深度为根节点到最远叶子节点的最长路径上的节点数
说明: 叶子节点是指没有子节点的节点
参考
补充知识:
- 深度
- 深度为根节点到最远叶子节点的最长路径上的节点数
- 上面示例中根节点3的深度为1
- 求深度用前序遍历
- 高度
- 该节点到叶子节点的最长简单路径边的条数或者节点数
- 上面示例根节点3的高度为3
- 求高度应该用后序遍历
- 实际上是高度和深度相同, 可用后序的方法求深度
思路: 递归实现
class Solution {
public:
int depth(TreeNode* root, int layer) {
if (root == nullptr) return layer;
int res1 = depth(root->left, layer + 1);
int res2 = depth(root->right, layer + 1);
return max(res1, res2);
}
int maxDepth(TreeNode* root) {
if (root == nullptr) return 0;
return depth(root, 0);
}
};
- 返回值为树的深度
- 终止条件: 确定为空节点的话返回0, 表示高度为0
- 单层递归: 求左子树深度, 再求右子树深度, 最后取左右深度最大数值+1
思路: 迭代法
使用层序遍历实现
111. 二叉树的最小深度
题解
对三种情况的处理:
- 左子树不为空, 右子树为空
- 左子树为空, 右子树不为空
- 左右子树都不为空
- 在叶子节点时, 左右子树都为空(默认递归会在此处返回)
思路: 递归法
class Solution {
public:
int depth = INT_MAX;
void myoperator(TreeNode* root, int layer) {
if (root == nullptr) return;
if (root->left == nullptr && root->right == nullptr) {
depth = min(layer, depth);
}
myoperator(root->left, layer + 1);
myoperator(root->right, layer + 1);
}
int minDepth(TreeNode* root) {
if (root == nullptr) return 0;
myoperator(root, 1);
return depth;
}
};
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) return 0; // 叶子节点的下一层, 相当于左右子树都不为空的情况返回
int res = 0;
if (root->left != nullptr && root->right == nullptr) {
res = 1 + minDepth(root->left);
} else if (root->left == nullptr && root->right != nullptr) {
res = 1 + minDepth(root->right);
} else {
res = 1 + min(minDepth(root->left), minDepth(root->right));//相当于左右子树都不为空的情况
}
return res;
}
};
class Solution {
public:
int minDepth(TreeNode* root) {
if (root == nullptr) return 0;
int res = 0;
if (root->left != nullptr && root->right == nullptr) { //左节点为空的情况
res = 1 + minDepth(root->left);
} else if (root->left == nullptr && root->right != nullptr) { //右节点为空的情况
res = 1 + minDepth(root->right);
} else if (root->left != nullptr && root->right != nullptr){ //左右都不为空的情况
res = 1 + min(minDepth(root->left), minDepth(root->right));
} else if (root->left == nullptr && root->right == nullptr) { //左右都为空的情况
res = 1 + min(minDepth(root->left), minDepth(root->right));
}
return res;
}
};
思路: 迭代法
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> myqueue;
if (root) myqueue.push(root);
int result = 0;
while (!myqueue.empty()) {
int size = myqueue.size();
result++;
for (int i = 0; i < size; i++) {
TreeNode* tem = myqueue.front();
myqueue.pop();
if (tem->left == nullptr && tem->right == nullptr) {
return result;
}
if (tem->left) myqueue.push(tem->left);
if (tem->right) myqueue.push(tem->right);
}
}
return result;
}
};
222.完全二叉树的节点个数
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树
:
除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。
若最底层为第 h 层,则该层包含 1~ 2 h 个节点。
思路: 层序遍历
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> myqueue;
int result = 0;
if (root) myqueue.push(root);
while (!myqueue.empty()) {
int size = myqueue.size();
for (int i = 0; i < myqueue.size(); i++) {
TreeNode* tem = myqueue.front();
myqueue.pop();
result++;
if (tem->left) myqueue.push(tem->left);
if (tem->right) myqueue.push(tem->right);
}
}
return result;
}
};
思路: 递归
class Solution {
public:
int countNodes(TreeNode* root) {
if (root == nullptr) return 0;
int count = 0;
count += countNodes(root->left);
count += countNodes(root->right);
count += 1; //注意这里是当前节点的计数, 加1 (非加2)
return count;
}
};
相关题目
104.二叉树的最大深度
559. N 叉树的最大深度
思路: 层序遍历实现
class Solution {
public:
int maxDepth(Node* root) {
int result = 0;
queue<Node*> myqueue;
if (root)
myqueue.push(root);
while (!myqueue.empty()) {
int size = myqueue.size();
result++;
for (int i = 0; i < size; i++) {
Node* tem = myqueue.front();
myqueue.pop();
for (int j = 0; j < tem->children.size(); j++) {
myqueue.push(tem->children[j]);
}
}
}
return result;
}
};
/*
// 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;
}
};
*/
思路: 递归实现