代码随想录算法训练营第十四天 | LeetCode513.找树左下角的值、LeetCode112.路径总和、LeetCode113.路径总和ii、LeetCode106.从中序与后序遍历序列构造二叉树、LeetCode105.从前序与中序遍历序列构造二叉树
01-1 LeetCode513.找树左小角的值
相关资源
题目链接:513. 找树左下角的值
文章讲解:找树左下角的值
题目:给定一个二叉树的 根节点 root
,请找出该二叉树的 最底层 最左边 节点的值。假设二叉树中至少有一个节点。
第一想法:层序遍历,记录每组出队的第一个元素,最后一层第一个元素就是二叉树最底层最左边的节点
实现:
class Solution {
public:
int findBottomLeftValue(TreeNode* root) {
int result;
queue<TreeNode*> que;
if(root!=nullptr){
que.push(root);
}
while(!que.empty()){
int size = que.size();
for(int i = 0; i < size; i++){
TreeNode* cur = que.front();
if(i==0){
result = cur->val;
}
if(cur->left){
que.push(cur->left);
}
if(cur->right){
que.push(cur->right);
}
que.pop();
}
}
return result;
}
};
看完代码随想录之后的想法: 首先确定最后一行,然后确定最左边的值。如果使用递归法,如何判断是最后一行呢,其实就是深度最大的叶子节点一定是最后一行。那么如何找最左边的呢?可以使用前序遍历(当然中序,后序都可以,因为本题没有 中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。
class Solution {
public:
int maxDepth = INT_MIN;
int result;
void traversal(TreeNode* root, int depth){
if(root->left==nullptr&&root->right==nullptr){
if(depth>maxDepth){
maxDepth = depth;
result = root->val;
}
}
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;
}
};
收获:递归求最大深度,涉及到回溯的过程
ToDo:复习递归实现方法
01-2 LeetCode112.路径总和
相关资源
- 题目链接:112. 路径总和
- 文章讲解:路径总和
- 视频讲解:LeetCode:112. 路径总和
题目:
给你二叉树的根节点 root
和一个表示目标和的整数 targetSum
。判断该树中是否存在 根节点到叶子节点 的路径,这条路径上所有节点值相加等于目标和 targetSum
。如果存在,返回 true
;否则,返回 false
。
叶子节点 是指没有子节点的节点。
第一想法:想到了前序遍历+回溯来计算路径长度
实现:
class Solution {
public:
int target;
bool traversal(TreeNode* root, int summary){
summary = summary + root->val;
bool left = false;
bool right = false;
if(root->left==nullptr&&root->right==nullptr){
if(summary==target){
return true;
}
else{
return false;
}
}
if(root->left){
left = traversal(root->left, summary);
}
if(root->right){
right = traversal(root->right, summary);
}
return (left || right);
}
bool hasPathSum(TreeNode* root, int targetSum) {
if(root==nullptr){
return false;
}
target = targetSum;
return traversal(root,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);
}
};
ToDo:复习并研究本题,弄清楚回溯这个玩意!
01-3 LeetCode113.路径总和ii
相关资源
- 题目链接:113. 路径总和 II
- 文章讲解:路径总和ii
题目:
给你二叉树的根节点 root
和一个整数目标和 targetSum
,找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。
叶子节点 是指没有子节点的节点。
第一想法:递归+回溯
实现:
class Solution {
public:
void traversal(TreeNode* root, vector<vector<int>>& result, vector<int>&record, int count){
if (root->left==nullptr&&root->right==nullptr&&count==0){
result.push_back(record);
return;
}
if(root->left){
count = count-(root->left->val);
record.push_back(root->left->val);
traversal(root->left,result,record,count);
count = count+(root->left->val);
record.pop_back();
}
if(root->right){
count = count-(root->right->val);
record.push_back(root->right->val);
traversal(root->right,result,record,count);
count = count+(root->right->val);
record.pop_back();
}
return;
}
vector<vector<int>> pathSum(TreeNode* root, int targetSum) {
vector<vector<int>> result;
vector<int>record;
if(root==nullptr){
return result;
}
record.push_back(root->val);
traversal(root,result,record,targetSum-root->val);
return result;
}
};
看完代码随想录之后的想法:和录哥思路一致
ToDo:复习
01-4 LeetCode106.从中序与后序遍历序列构造二叉树
相关资源
题目链接:106. 从中序与后序遍历序列构造二叉树
文章讲解:从中序与后序遍历序列构造二叉树
题目:
给定两个整数数组 inorder
和 postorder
,其中 inorder
是二叉树的中序遍历, postorder
是同一棵树的后序遍历,请你构造并返回这颗 二叉树 。
第一想法:完全没有思路
看完代码随想录之后的想法:构造二叉树的流程
- 第一步:如果数组大小为零的话,说明是空节点了。
- 第二步:如果不为空,那么取后序数组最后一个元素作为节点元素。
- 第三步:找到后序数组最后一个元素在中序数组的位置,作为切割点
- 第四步:切割中序数组,切成中序左数组和中序右数组 (顺序别搞反了,一定是先切中序数组)
- 第五步:切割后序数组,切成后序左数组和后序右数组
- 第六步:递归处理左区间和右区间
实现:
class Solution {
public:
TreeNode* traversal (vector<int>& inorder, vector<int>& postorder) {
if(postorder.size()==0){
return NULL;
}
int rootValue = postorder[postorder.size()-1];
TreeNode* root = new TreeNode(rootValue);
if(postorder.size()==1){
return root;
}
int indexOfInorder = 0;
for (indexOfInorder = 0; indexOfInorder < inorder.size(); indexOfInorder++) {
if (inorder[indexOfInorder] == rootValue) break;
}
vector<int> leftInorder(inorder.begin(), inorder.begin() + indexOfInorder);
vector<int> rightInorder(inorder.begin() + indexOfInorder + 1, inorder.end());
postorder.resize(postorder.size() - 1);
vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());
vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());
root->left = traversal(leftInorder, leftPostorder);
root->right = traversal(rightInorder, rightPostorder);
return root;
}
TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {
if (inorder.size() == 0 || postorder.size() == 0) return nullptr;
return traversal(inorder, postorder);
}
};
ToDo:复刻录哥代码
01-5 LeetCode105.从前序与中序遍历序列构造二叉树
相关资源
- 题目链接:105. 从前序与中序遍历序列构造二叉树
- 文章讲解:从前序与中序遍历序列构造二叉树
题目:
根据一棵树的前序遍历与中序遍历构造二叉树。
注意: 你可以假设树中没有重复的元素。
第一想法:类似从中序和后序遍历序列构造二叉树
实现:
class Solution {
public:
TreeNode* traversal (vector<int>& preorder, vector<int>& inorder) {
if(preorder.size()==0){
return NULL;
}
int rootValue = preorder[0];
TreeNode* root = new TreeNode(rootValue);
if(preorder.size()==1){
return root;
}
int indexOfInorder = 0;
for (indexOfInorder = 0; indexOfInorder < inorder.size(); indexOfInorder++) {
if (inorder[indexOfInorder] == rootValue) break;
}
vector<int> leftInorder(inorder.begin(), inorder.begin() + indexOfInorder);
vector<int> rightInorder(inorder.begin() + indexOfInorder + 1, inorder.end());
vector<int> leftPreorder(preorder.begin()+1, preorder.begin() + 1 + leftInorder.size());
vector<int> rightPreorder(preorder.begin() + leftInorder.size()+1, preorder.end());
root->left = traversal(leftPreorder, leftInorder);
root->right = traversal(rightPreorder, rightInorder);
return root;
}
TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {
if (inorder.size() == 0 || preorder.size() == 0) return nullptr;
return traversal(preorder, inorder);
}
};
ToDo:复习