一、题目打卡
1.1 找树左下角的值
题目链接:. - 力扣(LeetCode)
层序遍历属于固定的写法,没有很特别的。
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
q.push(root);
int res;
while(!q.empty()){
int size = q.size();
TreeNode* tmp = q.front();
res = tmp->val;
while(size--){
tmp = q.front();
q.pop();
if(tmp->left) q.push(tmp->left);
if(tmp->right) q.push(tmp->right);
}
}
return res;
}
};
看了答案,如果按照递归写,稍微的绕一点,主要是在每一层需要维护一个额外的递归的深度,并且注意终止的条件是遇到的第一个叶子节点,因为这里递归的开始是向左,所以遇到的第一个终止的节点一定是本层的最左边的节点。
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* root, int depth) {
if (root->left == NULL && root->right == NULL) {
if (depth > maxDepth) {
maxDepth = depth;
result = root->val;
}
return;
}
if (root->left) {
depth++;
traversal(root->left, depth);
depth--; // 回溯
}
if (root->right) {
depth++;
traversal(root->right, depth);
depth--; // 回溯
}
return;
}
int findBottomLeftValue(TreeNode* root) {
traversal(root, 0);
return result;
}
};
1.2 路径总和
题目链接:. - 力扣(LeetCode)
题目做的过程中采用了累加的思想和递归会存储数值的特点来做的:
class Solution {
public:
bool recur(TreeNode* root,int &targetSum, int sum){
if(!root) return false;
sum += root->val;
if(!root->left && !root->right && sum == targetSum) return true;
if(!root->left && !root->right && sum != targetSum) return false;
return recur(root->left, targetSum, sum) || recur(root->right, targetSum, sum);
}
int sum = 0;
bool hasPathSum(TreeNode* root, int targetSum) {
// if(!root) return false;
// sum += root->val;
// if(!root->left && !root->right && sum == targetSum){
// sum -= root->val;
// return true;
// }
// if(!root->left && !root->right && sum != targetSum){
// sum -= root->val;
// return false;
// }
// sum -= root->val;
// return hasPathSum(root->left,targetSum) || hasPathSum(root->right,targetSum);
return recur(root,targetSum,0);
}
};
注释的部分是自己踩坑的内容,首先遇到的错误是非叶子节点的情况返回值并没有将 sum 减去当前节点的值,然后加了 sum -= root->val; 这句话,但是这样还是存在问题,因为这样的话,在判断不是最后的叶子节点的时候,会把当前的值直接删掉,导致 sum 没有正确被维护,所以这种回溯的题目,在参数列表维护当前需要的值是比较可取的方法,全局变量容易出错。
仔细思考以后,发现这里其实关键在于我处理的时候,sum -= root-val; 这个语句应该是在递归以后进行,所以应该暂存一下那两个数据:
class Solution {
public:
int sum = 0;
// bool recur(TreeNode* root,int &targetSum, int sum){
// if(!root) return false;
// sum += root->val;
// if(!root->left && !root->right && sum == targetSum) return true;
// if(!root->left && !root->right && sum != targetSum) return false;
// return recur(root->left, targetSum, sum) || recur(root->right, targetSum, sum);
// }
bool hasPathSum(TreeNode* root, int targetSum) {
if(!root) return false;
sum = sum + root->val;
// cout << sum << endl;
if(!root->left && !root->right && sum == targetSum){
sum -= root->val;
return true;
}
if(!root->left && !root->right && sum != targetSum){
sum -= root->val;
return false;
}
bool f1 = hasPathSum(root->left,targetSum);
bool f2 = hasPathSum(root->right,targetSum);
sum -= root->val;
return f1 || f2;
// return recur(root,targetSum,0);
}
};
答案的话直接是进行递减的操作,更为简洁:
class Solution {
private:
bool traversal(TreeNode* cur, int count) {
if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0
if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回
if (cur->left) { // 左
count -= cur->left->val; // 递归,处理节点;
if (traversal(cur->left, count)) return true;
count += cur->left->val; // 回溯,撤销处理结果
}
if (cur->right) { // 右
count -= cur->right->val; // 递归,处理节点;
if (traversal(cur->right, count)) return true;
count += cur->right->val; // 回溯,撤销处理结果
}
return false;
}
public:
bool hasPathSum(TreeNode* root, int sum) {
if (root == NULL) return false;
return traversal(root, sum - root->val);
}
};
1.3 路径之和ii
和上面那个题其实是一样的逻辑:
class Solution {
private:
int sum(vector<int> &tmp){
int res = 0;
for(auto & it : tmp){
res += it;
}
return res;
}
void recur(TreeNode* root, int &targetSum, vector<int> &tmp, vector<vector<int>> &res){
if(!root) return;
tmp.push_back(root->val);
if(!root->left && !root->right && sum(tmp) == targetSum){
res.push_back(tmp);
tmp.pop_back();
return;
}
recur(root->left,targetSum,tmp,res);
recur(root->right,targetSum,tmp,res);
tmp.pop_back();
}
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(!root) return {};
vector<int> tmp;
vector<vector<int>> res;
recur(root,targetSum,tmp,res);
return res;
}
};
通过这个题,随想录总结了关于写递归函数什么时候需要返回值,什么时候不需要:
- 如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值。(这种情况就是本文下半部分介绍的113.路径总和ii)
- 如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。 (这种情况我们在236. 二叉树的最近公共祖先 (opens new window)中介绍)
- 如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回。(本题的情况)
1.4 从后序与后序遍历序列构造二叉树(借助答案思路)
题目链接:. - 力扣(LeetCode)
这个题是第一次接触,有一点没思路,直接看了答案的解析,然后写了一版:
class Solution {
public:
TreeNode* recur(vector<int>& in, int inBegin, int inEnd, vector<int> &post, int postBegin, int postEnd){
if(postEnd - postBegin <= 0) return nullptr;
int last = post[postEnd - 1];
TreeNode* node = new TreeNode(last);
int seg = inBegin;
for(;seg < inEnd;seg++){
if(in[seg] == last) break;
}
if(postEnd - postBegin == 1) return node;
// cout << "------------" <<endl;
// cout << "inBegin: " << inBegin << " seg: " << seg << " postBegin: " << postBegin << " postBegin + seg - inBegin: " << postBegin + seg - inBegin << endl;;
// cout << "seg + 1: " << seg + 1 << " inEnd: " << inEnd << " postBegin + seg : " << postBegin + seg << " postEnd - 1 : " << postEnd - 1 << endl;
node->left = recur(in,inBegin,seg,post,postBegin,postBegin + seg - inBegin);
node->right = recur(in,seg + 1,inEnd,post,postBegin + seg - inBegin,postEnd - 1);
return node;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
int len = inorder.size();
return recur(inorder,0,len,postorder,0,len);
}
};
有点类似归并排序,这里写的过程中出现了一个问题,经过日志输出排除的,就是对于分割后序的数组而言,由于是按照中序数组分割点的位置来确定后序数组分割的大小,而分割时候的中序数组的左索引是变化的,所以这里后序数组的一定需要减去的是由切割点到中序数组起始点的这个步长,其他的就主要是理解这个题目的过程:
-
第一步:如果数组大小为零的话,说明是空节点了。
-
第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
-
第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
-
第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
-
第五步:切割后序数组,切成后序左数组和后序右数组
-
第六步:递归处理左区间和右区间