104.二叉树的最大深度
解题思路:
如果说我们我们想得到的是他的深度,我们是不是应该选择前序遍历序列,也就是中左右的遍历顺序,因为只有这样我们才能计算他的深度,但是在网络上搜索会发现大多数题解用的都是一种后序的遍历,那么后序遍历求得是什么呢?后序遍历一般就是从叶子节点求起,他求的是二叉树的高度,仔细研究会发现二叉树的高度和他的深度好像是一样的,我们可以用递归的方法来实现它高度的计算。
/**
* 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 maxDepth(TreeNode* root) {
if(root == NULL) return 0;
int lefthigh = maxDepth(root->left);//左
int righthigh = maxDepth(root->right);//右
int maxheight = 1 + max(lefthigh,righthigh);//中
return maxheight;
}
};
那么用前序遍历到底是否能解,答案是肯定的,因为前序遍历才是求深度的一个逻辑,但是前序遍历求深度的算法要比后序要复杂得多,涉及到了回溯算法。
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;
}
};
111.二叉树的最小深度
题目中定义最小深度是从根节点到最近叶子节点的最短路径上的节点数量。前一个题我们刚求了二叉树的最大深度,那么可不可以将上个题中的求最大深度直接改成求最小深度,答案是不可以的因为如果直接改成最小深度,那么返回的答案就是左右子树的最小深度加1,如果左子树为空或右子树为空就将空的结点也算进去了,最终最小深度就为1了,所以我们在算的时候应该求右子树的最小深度加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 minDepth(TreeNode* root) {
if(root == NULL) return 0;
int lefthigh = minDepth(root->left);//左
int righthigh = minDepth(root->right);//右
if(root->left == NULL && root->right != NULL){
return 1 + righthigh;
}
if(root->left != NULL && root->right == NULL){
return 1 + lefthigh;
}
int minheight = 1 + min(lefthigh,righthigh);//中
return minheight;
}
};
222.完全二叉树的结点个数
222. 完全二叉树的节点个数 - 力扣(LeetCode)
解题思路:
这道题我们完全可以用普通二叉树的遍历去解决它,论代码相对比较容易的是前中后序的递归遍历
/**
* 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 getnum(TreeNode* root){
if(root == NULL) return 0;
int numleft = getnum(root->left);
int numright = getnum(root->right);
int result = numleft + numright + 1;
return result;
}
int countNodes(TreeNode* root) {
return getnum(root);
}
};
但是我们要是按着完全二叉树(见上图)的思路去做这个题的话,可以先判断二叉树的左右子树是否存在满二叉树的情况,如果有就返回满二叉树的节点个数,满二叉树的节点个数是2的深度次方减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;
TreeNode* left = root->left;
TreeNode* right = root->right;
int leftDepth = 0, rightDepth = 0; // 这里初始为0是有目的的,为了下面求指数方便
while (left) { // 求左子树深度
left = left->left;
leftDepth++;
}
while (right) { // 求右子树深度
right = right->right;
rightDepth++;
}
if (leftDepth == rightDepth) {//当左右子树深度相等时,就是满二叉树就可以用满二叉树的节点个数公式求
return (2 << leftDepth) - 1; // 注意(2<<1) 相当于2^2,所以leftDepth初始为0
}
return countNodes(root->left) + countNodes(root->right) + 1;
}
};