一、LeetCode题目
1.LeetCode513:找树左下角的值
题目链接:513. 找树左下角的值 - 力扣(LeetCode)
题目解析
毫无疑问,个人认为层序遍历是最好的方法,好理解,好写;不过这里贴出了学习了用回溯搜索去写,其实就可以理解成,按照指定顺序遍历树,然后在中间满足一定条件更新变量。相当于高级的for循环(bushi),同时也优化了一个比较极致的递归版本,改变了递归条件和递归顺序。
回溯c++代码如下:
-
/**
-
* 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 max_depth = INT_MIN;
-
int target = 0;
-
// 设定深度搜索函数(其实是回溯函数)
-
void dfs(TreeNode* root, int depth) {
-
if (!root)
-
return;
-
// 需要的条件:左节点,深度更大。注意!只有满足条件才会更新,要不一直在遍历回溯
-
if (!root->left && !root->right && max_depth < depth) {
-
max_depth = depth;
-
target = root->val;
-
// 注意不要return,因为要一直更新遍历
-
}
-
// 左子树检查
-
if (root->left) {
-
dfs(root->left, depth + 1);
-
}
-
// 右子树检查
-
if (root->right) {
-
dfs(root->right, depth + 1);
-
}
-
return;
-
}
-
int findBottomLeftValue(TreeNode* root) {
-
dfs(root, 0);
-
return target;
-
}
-
};
回溯加强版c++代码:
-
class Solution {
-
public:
-
int findBottomLeftValue(TreeNode* root) {
-
int bottomLeftValue = 0;
-
int maxDepth = -1;
-
dfs(root, 0, maxDepth, bottomLeftValue);
-
return bottomLeftValue;
-
}
-
void dfs(TreeNode* node, int depth, int& maxDepth, int& bottomLeftValue) {
-
if (!node) return;
-
// 如果当前深度比已知最大深度大,或者当前节点是左节点,更新最大深度和左下角值
-
if (depth > maxDepth || (depth == maxDepth && node->left)) {
-
maxDepth = depth;
-
bottomLeftValue = node->val;
-
}
-
// 先遍历右子树,这样左子树的节点会在相同深度下覆盖右子树的节点
-
if (node->right) dfs(node->right, depth + 1, maxDepth, bottomLeftValue);
-
if (node->left) dfs(node->left, depth + 1, maxDepth, bottomLeftValue);
-
}
-
};
层序遍历c++代码:
-
/**
-
* 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:
-
queue<TreeNode*> que;
-
vector<vector<int>> result;
-
int findBottomLeftValue(TreeNode* root) {
-
que.push(root);
-
int size = que.size();
-
while (!que.empty())
-
{
-
vector<int> cur_vec;
-
while (size--)
-
{
-
TreeNode* cur_node = que.front();
-
que.pop();
-
cur_vec.push_back(cur_node->val);
-
if (cur_node->left)
-
que.push(cur_node->left);
-
if (cur_node->right)
-
que.push(cur_node->right);
-
}
-
result.push_back(cur_vec);
-
size = que.size();
-
}
-
return result.back()[0];
-
}
-
};
2.Leetcode112:路径总和
题目解析
利用回溯的思想,在遇到叶子节点且满足总和条件的前提下,返回为真。左右子树都检索,最后取一个或,因为返回只要求返回bool只检查又或者没有符合条件路径。
C++代码如下:
-
/**
-
* 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:
-
// 回溯搜索路径
-
bool backtracking(TreeNode* root, int targetSum) {
-
if (!root)
-
return false;
-
if (targetSum == root->val && !root->right && !root->left)
-
return true;
-
// backtracking(root->left,targetSum-root->val);
-
// backtracking(root->right,targetSum - root->val);
-
return backtracking(root->left, targetSum - root->val) ||
-
backtracking(root->right, targetSum - root->val);
-
}
-
bool hasPathSum(TreeNode* root, int targetSum) {
-
return backtracking(root, targetSum);
-
}
-
};
注意点:这里采用的是值传入,所以没有用显式的回溯过程,那么检验的条件也应该是targetSum == root->val,而不是等于0.
3.Leetcode106:从中序与后序遍历序列构造二叉树
题目链接:106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
题目解析
大致的思路是:根据后序遍历的结果锁定根节点(末尾),然后用这个值去中序红寻找并切分,这时候问题就会转移到如何根据剩下的这些条件开启下一轮的切分们也就是递归。利用合理的区间构建好左右中序数组之后,可以利用他们的相同长度去切分后序数组,这样就可以得到下一轮的数组。那么根据这个过程,对应的中止条件就是:数组无法切分,即数组为空为止。
C++代码如下:
-
/**
-
* 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:
-
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
-
// 确定中止条件
-
if (!inorder.size() && !postorder.size())
-
return nullptr;
-
// 取后序数组的最后一个分割中序数组
-
int root_num = postorder.back();
-
int root_post = 0;
-
for (int i = 0; i < inorder.size(); ++i) {
-
if (inorder[i] == root_num) {
-
root_post = i;
-
break;
-
}
-
}
-
// 创造根节点
-
TreeNode* root = new TreeNode(root_num);
-
// 分割为左数组和右数组
-
vector<int> inorder_left =
-
vector<int>(inorder.begin(), inorder.begin() + root_post);
-
vector<int> inorder_right =
-
vector<int>(inorder.begin() + root_post + 1, inorder.end());
-
// 根据后序和中序遍历大小是一样的
-
vector<int> postorder_left = vector<int>(
-
postorder.begin(), postorder.begin() + inorder_left.size());
-
vector<int> postorder_right = vector<int>(
-
postorder.begin() + inorder_left.size(),
-
postorder.begin() + inorder_left.size() + inorder_right.size());
-
// 一层逻辑已经结束,写递归逻辑
-
root->left = buildTree(inorder_left, postorder_left);
-
root->right = buildTree(inorder_right, postorder_right);
-
return root;
-
}
-
};
4.Leetcode105:从前序与中序遍历序列构造二叉树
题目链接:105. 从前序与中序遍历序列构造二叉树 - 力扣(LeetCode)
题目解析
和后序的思路基本上是一致的,最关键的就是怎么合理的处理范围,不要超出范围或者范围混乱。
C++代码如下:
-
/**
-
* 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:
-
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
-
if (!preorder.size() && !inorder.size())
-
return nullptr;
-
// 获取前序数组的第一个元素
-
int val = preorder.front();
-
preorder.erase(preorder.begin());
-
// 创建新节点
-
TreeNode* root = new TreeNode(val);
-
int pos = 0;
-
// 在中序数组中寻找位置
-
for (int i = 0; i < inorder.size(); ++i) {
-
if (inorder[i] == val) {
-
pos = i;
-
}
-
}
-
// 根据获取元素分割中序数组
-
vector<int> inorder_left =
-
vector<int>(inorder.begin(), inorder.begin() + pos);
-
vector<int> inorder_right =
-
vector<int>(inorder.begin() + pos + 1, inorder.end());
-
// 切分前序数组
-
vector<int> preorder_left = vector<int>(
-
preorder.begin(), preorder.begin() + inorder_left.size());
-
vector<int> preorder_right =
-
vector<int>(preorder.begin() + inorder_left.size(), preorder.end());
-
root->left = buildTree(preorder_left, inorder_left);
-
root->right = buildTree(preorder_right, inorder_right);
-
return root;
-
}
-
};
注意点1:中止条件检查一个就够了,因为题目给出的遍历是合法的,所以两个数组的长度也一定一样。
注意点2:切分的时候注意迭代器写法是左闭右开,而且中止迭代器是在下一位;还有就是跨过切分点(根节点),选取区间不是连续的。
总结
补打卡第16天,坚持!!!