1.二叉树的最大深度:
完全二叉树最底层的节点不完全在根节点的左子树,比如下面那个就是完全二叉树。
深度是根节点到某个节点 最长的路径上的节点数或者条数(取决于深度从0开始还是从1开始)
假设根节点深度为1,越往下越大。从上往下,用前序遍历。
高度是某节点到叶子节点最长的路径上的节点数或者条数(取决于深度从0开始还是从1开始)
假设叶子节点为1,越往上越大,从下往上,用后序遍历
但是根节点的高度就是二叉树的最大深度,所以前序后序都可以。
递归(后续遍历):
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(treenode* root) {
return getdepth(root);
}
};
先到最下面那层,那层的高度为1,再往上,上一层depth加1,然后取最大值。
递归(前序遍历):
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;
}
};
问:在遍历完根节点的左子树后,回到node为根节点的那次递归,接下来应该开始判断根节点的右子树,我想问一下这时的depth是1还是遍历完左子树后的depth?
答:是1,因为depth是传值调用,具体见函数(形参与实参、函数递归);
迭代法:
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叉树的最大深度
递归法:
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()) {
int 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;
}
};
思路都是一样的,就是其中的操作不一样,比如往队列里加节点操作不一样:
for(int j=0;j<node->children.size();j++){
if(node->children[j]) que.push(node->children[j]);
}
2.二叉树的最小深度:
最小深度是从根节点到最近叶子节点的最短路径上的节点数量,为什么是叶子节点,因为最小深度就是到某个没有子节点的节点,这个就是叶子节点。
递归法( 后序遍历):
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);
}
};
把每一个叶子节点的高度当做1,开始往上,最后交在一起取最小加1(加1是因为根节点)。
递归法(前序遍历):
class Solution {
private:
int result;
void getdepth(TreeNode* node, int depth) {
if (node->left == NULL && node->right == NULL) {
result = min(depth, result);
return;
}
// 中 只不过中没有处理的逻辑
if (node->left) { // 左
getdepth(node->left, depth + 1);
}
if (node->right) { // 右
getdepth(node->right, depth + 1);
}
return ;
}
public:
int minDepth(TreeNode* root) {
if (root == NULL) return 0;
result = INT_MAX;
getdepth(root, 1);
return result;
}
};
遍历到每一个叶子节点,根据当前depth和之前遍历的result比较,小的话,result=depth,否则返回,继续遍历到其他叶子节点,因为要比较谁小,所以result初始值要最大。
迭代法:
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++) {
TreeNode* 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;
}
};
就比二叉树的最大深度多一句代码。
3.完全二叉树的节点个数:
递归法:
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:
int countNodes(TreeNode* root) {
return getNodesNum(root);
}
};
其实没必要和他一样:
class Solution {
public:
int countNodes(TreeNode* root) {
if(root==nullptr) return 0;
int leftNumber=countNodes(root->left);
int rightNumber=countNodes(root->right);
return leftNumber+rightNumber+1;
}
};
这样更简洁;
迭代法:
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;
}
};
初始话result时是0,别弄成1了,在第一次while循环时会把root节点算上去的。