513. 找树左下角的值
一、做题感受&第一想法
层序遍历,记录下每层最左的元素(第一个元素)。
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
queue<TreeNode*> q;
TreeNode* cur = nullptr;
int result = 0;
if(root) q.push(root);
while(!q.empty()){ //层序遍历
int size = q.size();
result = q.front()->val; //记录下每一层的第一个元素,也就是最左元素
for(int i = 0;i < size;i++){
cur = q.front();
q.pop();
if(cur->left) q.push(cur->left);
if(cur->right) q.push(cur->right);
}
}
return result;
}
};
二、学习文章后收获
1.递归法
要点:
- 深度问题->前序遍历
- 如何找到最左的节点?->无论是前序遍历、后序、中序遍历,左都先于右,所以“同一深度”下先被遍历的永远是最左节点。
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int currentDepth = 0;
TreeNode* currentRoot = nullptr;
traversal(root, 1,currentRoot,currentDepth);
return currentRoot->val;
}
void traversal(TreeNode* root, int depth, TreeNode* ¤tRoot, int ¤tDepth){
if(root->left == nullptr && root->right == nullptr){ //叶子
if(currentDepth < depth){
currentDepth = depth;
currentRoot = root;
}
}
if(root->left) traversal(root->left, depth + 1, currentRoot, currentDepth);
if(root->right) traversal(root->right, depth + 1, currentRoot, currentDepth);
return;
}
};
2.如果把上述改成带回溯细节的代码
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;
}
};
112. 路径总和
一、做题感受&第一想法
类似于“返回所有的路径”的那题,属于深度问题,应该用前序遍历。
class Solution {
public:
bool hasPathSum(TreeNode* root, int targetSum) {
int currentSum = 0;
if(root) return find(root,targetSum,currentSum);
else return false;
}
bool find(TreeNode* root, int targetSum, int currentSum){ //如果找到了,则返回true
currentSum += root->val; //计算当前currentSum
if(root->left == nullptr && root->right == nullptr){ //叶子
if(currentSum == targetSum) return true;
else return false;
}
bool left, right;
if(root->left) left = find(root->left, targetSum, currentSum);
if(root->right) right = find(root->right,targetSum,currentSum);
return left || right;
}
};
二、学习文章后收获
1.另一种版本的代码
关注点:
- 没有采用逐步相加的计数器,而是一次次减掉val,观察到叶子节点时,是否已经减少到0。
hasPathSum(root->left, sum - root->val)
中,sum - root->val
隐藏了回溯。
class Solution {
public:
bool hasPathSum(TreeNode* root, int sum) {
if (!root) return false;
if (!root->left && !root->right && sum == root->val) {
return true;
}
return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);
}
};
2.迭代法(暂时没看,因为pair不熟悉,后续补上)
三、相似题目推荐
1.递归+回溯
class Solution {
public:
vector<vector<int>> result;
vector<int> path;
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
if(root){
path.push_back(root->val);
find(root,targetSum - root->val);
}
return result;
}
void find(TreeNode* root, int sum){
if(root->left == NULL && root->right == NULL){
if(sum == 0){
result.push_back(path);
}
else return;
}
if(root->left){
path.push_back(root->left->val);
sum = sum - root->left->val;
find(root->left, sum);
sum = sum + root->left->val;
path.pop_back();
}
if(root->right){
path.push_back(root->right->val);
sum = sum - root->right->val;
find(root->right, sum);
sum = sum + root->right->val;
path.pop_back();
}
return;
}
};
2.递归
class Solution {
public:
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> result;
vector<int> path;
if(root) findPath(root, targetSum, path, result);
return result;
}
void findPath(TreeNode* root, int currentSum, vector<int> path, vector<vector<int>> &result){
currentSum -= root->val;
path.push_back(root->val);
if(!root->left && !root->right && currentSum == 0){
result.push_back(path);
return;
}
if(root->left) findPath(root->left, currentSum, path, result);
if(root->right) findPath(root->right, currentSum, path, result);
return;
}
};
106.从中序与后序遍历序列构造二叉树
一、做题感受&第一想法
看了代码随想录的手算方法,有了思路,最后ac。
思路:迭代法:(前序遍历)
- 返回值和函数参数:返回下层子树的根节点(下层子树已经建好)。函数参数传入每次需要建立的子树的中序、后序序列。
- 返回条件:当前子树的中序、后序序列都为空,则返回null。
- 本层处理:①分配空间建立本层根节点。②切分两个序列:取出后序遍历序列的最后一个元素,即为根节点。根据根节点,切分中序遍历序列为左、右两个子树的中序序列,同时,按照这两个序列的长度,对应切分好后序遍历序列为左、右两个子树的后序序列。(切分完毕后,作为函数参数传给下层递归函数。)
class Solution {
public:
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if(inorder.size() == 0) return nullptr;
TreeNode* root = new TreeNode;
root->val = postorder.back();
int temp = postorder.back();
vector<int> inOrderLeft, inOrderRight;
vector<int> postOrderLeft, postOrderRight;
int i = 0;
int size = inorder.size();
for(i = 0;i < size;i++){
if(inorder[i] == temp) break;
inOrderLeft.push_back(inorder[i]);
postOrderLeft.push_back(postorder[i]);
}
i++;
while(i < size){
inOrderRight.push_back(inorder[i]);
postOrderRight.push_back(postorder[i-1]);
i++;
}
root->left = buildTree(inOrderLeft,postOrderLeft);
root->right = buildTree(inOrderRight,postOrderRight);
return root;
}
};
二、学习文章后收获
1.切割vector的小方法
记录下切割点下标delimeterIndex
,然后利用vector本身的构造函数
一定一定要注意:
- vector的构造函数需要传入**”左闭右开“**的两个迭代器!!
- vector的构造函数不能传入下标,要传迭代器!(但是因为vector支持随机存取,所以迭代器可以加减整数值)
- 不要忘记把后序遍历最后一个元素去掉!
// 找到中序遍历的切割点
int delimiterIndex;
for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {
if (inorder[delimiterIndex] == rootValue) break;
}
// 左闭右开区间:[0, delimiterIndex)
vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);
// [delimiterIndex + 1, end)
vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );
// postorder 舍弃末尾元素,因为这个元素就是中间节点,已经用过了
postorder.resize(postorder.size() - 1);
// 左闭右开,注意这里使用了左中序数组大小作为切割点:[0, leftInorder.size)
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
// [leftInorder.size(), end)
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
2.一个注意点
- 中序数组大小一定是和后序数组的大小相同的!
3.更好性能的代码(待补充,请自己写一遍!!)
上述代码每次都需要传入vector作为参数,但是实际上我们可以直接传入下标作为参数!
//to be continue