当前位置: 首页 > news >正文

网站链接失效怎么做黄金网站app大全

网站链接失效怎么做,黄金网站app大全,网站数据分析指标,建设政府网站可行性报告刷题的第十五天,希望自己能够不断坚持下去,迎来蜕变。😀😀😀 刷题语言:C Day15 任务 ● 513.找树左下角的值 ● 112. 路径总和 113.路径总和ii ● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历…

刷题的第十五天,希望自己能够不断坚持下去,迎来蜕变。😀😀😀
刷题语言:C++
Day15 任务
● 513.找树左下角的值
● 112. 路径总和 113.路径总和ii
● 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树

1 找树左下角的值

在这里插入图片描述
本题要找出树的最后一行最左边的值
思路1:层序遍历
思路2:递归

迭代法
层序遍历模板参考代码随想录刷题题Day12

class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> que;int result;if (root != NULL) que.push(root);while (!que.empty()){int size = que.size();for (int i = 0; i < size; i++){TreeNode* node = que.front();que.pop();if (i == 0) result = node->val;// 记录最后一行第一个元素if (node->left) que.push(node->left);if (node->right) que.push(node->right);}}return result;}
};

递归法

误区:不是一直向左遍历,最后一个就是答案
一直向左遍历到最后一个,未必是最后一行

关键:在树的最后一行找到最左边的值

(1) 判断最后一行:深度最大的叶子节点
(2) 最左边的值:可以使用前序遍历(当然中序,后序都可以,因为本题没有 中间节点的处理逻辑,只要左优先就行),保证优先左边搜索,然后记录深度最大的叶子节点,此时就是树的最后一行最左边的值。

(1)确定递归函数的参数和返回值
参数:要遍历的树的根节点,最长深度
返回值:void

int maxDepth = INT_MIN;// 全局变量 记录最大深度
int result;            // 全局变量 最大深度最左节点的数值
void traversal(TreeNode* node, int depth)

(2)确定终止条件

当遇到叶子节点的时候,就需要统计一下最大的深度了,所以需要遇到叶子节点来更新最大深度。

if (node->left == NULL && node->right == NULL)
{if (depth > maxDepth){maxDepth = depth;   // 更新最大深度result = node->val; // 最大深度最左面的数值}return;
}

(3)确定单层递归的逻辑

找最大深度的时候,递归的过程中依然要使用回溯

// 中
if (node->left) {// 左depth++;// 深度加一traversal(node->left, depth);depth--;// 回溯,深度减一
}
if (node->right) {// 右depth++;// 深度加一traversal(node->right, depth);depth--;// 回溯,深度减一
}

C++:

class Solution {
public:int maxDepth = INT_MIN;int result;void traversal(TreeNode* node, int depth) {if (node->left == NULL && node->right == NULL) {if (maxDepth < depth) {maxDepth = depth;result = node->val;}}if (node->left) {depth++;traversal(node->left, depth);depth--;}if (node->right) {depth++;traversal(node->right, depth);depth--;}return;}int findBottomLeftValue(TreeNode* root) {traversal(root, 0);return result;}
};

精简版本C++:

class Solution {
public:int maxDepth = INT_MIN;int result;void traversal(TreeNode* node, int depth) {if (node->left == NULL && node->right == NULL) {if (maxDepth < depth) {maxDepth = depth;result = node->val;}}if (node->left) {traversal(node->left, depth + 1);// 隐藏着回溯}if (node->right) {traversal(node->right, depth + 1);// 隐藏着回溯}return;}int findBottomLeftValue(TreeNode* root) {traversal(root, 0);return result;}
};

2 路径总和

在这里插入图片描述
思路:

使用深度优先遍历的方式,本题前中后序都可以,因为中间节点没有处理逻辑

递归法
(1)确定递归函数的参数和返回类型

参数:二叉树的根节点、计算器(用来计算二叉树的一条边之和是否正好是目标和)
返回值:要找一条符合条件的路径,所以递归函数需要返回值,遍历的路线,并不要遍历整棵树,及时返回,返回类型是bool
在这里插入图片描述

递归函数返回值:
(1)如果需要搜索整棵二叉树且不用处理递归返回值,递归函数就不要返回值
(2)如果需要搜索整棵二叉树且需要处理递归返回值,递归函数就需要返回值。
(3)如果要搜索其中一条符合条件的路径,那么递归一定需要返回值,因为遇到符合条件的路径了就要及时返回

bool traversal(TreeNode* node, int count)

(2)确定终止条件

计数器count初始为目标和,然后每次减去遍历路径节点上的数值

  1. 如果最后count == 0,同时到了叶子节点的话,说明找到了目标和
  2. 如果遍历到了叶子节点,count不为0,就是没找到
if (node->left == NULL && node->right == NULL && count == 0) return true;
if (node->left == NULL && node->right == NULL && count != 0) return false;

(3)确定单层递归的逻辑

递归函数是有返回值的,如果递归函数返回true,说明找到了合适的路径,应该立刻返回

if (node->left) {// 左 (空节点不遍历)// 遇到叶子节点返回true,则直接返回trueif (traversal(node->left, count - node->left->val)) return true;
}
if (node->right) {// 右 (空节点不遍历)// 遇到叶子节点返回true,则直接返回trueif (traversal(node->right, count - node->right->val)) return true;
return false;

把回溯的过程表现出来:

if (node->left) {// 左count -= node->left->val;// 递归,处理节点;if (traversal(node->left, count)) return true;count += node->left->val;// 回溯,撤销处理结果
}
if (node->right) { // 右count -= node->right->val;if (traversal(node->right, count)) return true;count += node->right->val;// 回溯,撤销处理结果
}

C++:

class Solution {
public:bool traversal(TreeNode* node, int count) {if (node->left == NULL && node->right == NULL && count == 0) return true;// 遇到叶子节点,并且计数为0if (node->left == NULL && node->right == NULL && count != 0) return false;// 遇到叶子节点直接返回if (node->left) {// 左count -= node->left->val;// 递归,处理节点;if (traversal(node->left, count)) return true;count += node->left->val;// 回溯,撤销处理结果}if (node->right) {// 右count -= node->right->val;// 递归,处理节点if (traversal(node->right, count)) return true;count += node->right->val;// 回溯,撤销处理结果}return false;}bool hasPathSum(TreeNode* root, int targetSum) {if (root == NULL) return false;return traversal(root, targetSum - root->val);}
};

精简版本C++:

class Solution {
public:bool hasPathSum(TreeNode* root, int targetSum) {if (!root) return false;if (!root->left && !root->right && targetSum == root->val) {return true;}return hasPathSum(root->left, targetSum - root->val) || hasPathSum(root->right, targetSum - root->val);}
};

在这里插入图片描述
思路:
路径总和ii要遍历整个树,找到所有路径,所以递归函数不要返回值
在这里插入图片描述

class Solution {
public:vector<vector<int>> result;vector<int> path;// 递归函数不需要返回值,因为我们要遍历整个树void traversal(TreeNode* node, int count) {if (node->left == NULL && node->right == NULL && count == 0) {result.push_back(path);return;}if (node->left == NULL && node->right == NULL) return;// 遇到叶子节点而没有找到合适的边,直接返回if (node->left) {// 左 (空节点不遍历)path.push_back(node->left->val);count -= node->left->val;traversal(node->left, count);// 递归count += node->left->val;// 回溯path.pop_back();// 回溯}if (node->right) {// 右 (空节点不遍历)path.push_back(node->right->val);count -= node->right->val;traversal(node->right, count);// 递归count += node->right->val;// 回溯path.pop_back();// 回溯}return;}vector<vector<int>> pathSum(TreeNode* root, int targetSum) {result.clear();path.clear();if (root == NULL) return result;path.push_back(root->val);// 把根节点放进路径traversal(root, targetSum - root->val);return result;}
};

3 从中序与后序遍历序列构造二叉树

在这里插入图片描述
思路:

  1. 后序数组为0,空节点
  2. 后序数组最后一个元素为节点元素
  3. 寻找中序数组位置作为切割点
  4. 切中序数组
  5. 切后序数组
  6. 递归处理左右区间

在这里插入图片描述
C++:

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 index;for (index = 0; index < inorder.size(); index++){if (inorder[index] == rootValue) break;}// 切割中序数组vector<int> leftInorder(inorder.begin(), inorder.begin() + index);vector<int> rightInorder(inorder.begin() + index + 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 NULL;return traversal(inorder, postorder);}
};

4 从前序与中序遍历序列构造二叉树

在这里插入图片描述
思路:

  1. 前序数组为0,空节点
  2. 前序数组第一个元素为节点元素
  3. 寻找中序数组位置作为切割点
  4. 切中序数组
  5. 切前序数组
  6. 递归处理左右区间

C++:

class Solution {
public:TreeNode* traversal(vector<int>& preorder, vector<int>& inorder) {// 前序数组为0,空节点if (preorder.size() == 0) return NULL;// 前序数组第一个元素为节点元素int rootValue = preorder[0];TreeNode* root = new TreeNode(rootValue);if (preorder.size() == 1) return root;// 寻找中序数组位置作为切割点int index;for (index = 0; index < inorder.size(); index++) {if (inorder[index] == rootValue) break;}// 切中序数组vector<int> leftInorder(inorder.begin(), inorder.begin() + index);vector<int> rightInorder(inorder.begin() + index + 1, inorder.end());// 切前序数组preorder.erase(preorder.begin());vector<int> leftPreorder(preorder.begin(), preorder.begin() + leftInorder.size());vector<int> rightPreorder(preorder.begin() + leftPreorder.size(), preorder.end());// 递归处理左右区间root->left = traversal(leftPreorder, leftInorder);root->right = traversal(rightPreorder, rightInorder);return root;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {if (preorder.size() == 0 || inorder.size() == 0) return NULL;return traversal(preorder, inorder);}
};

鼓励坚持十六天的自己😀😀😀


文章转载自:
http://dinncoindefinitive.ssfq.cn
http://dinncocounterviolence.ssfq.cn
http://dinncolouis.ssfq.cn
http://dinncodcvo.ssfq.cn
http://dinncomoorbird.ssfq.cn
http://dinncostradivarius.ssfq.cn
http://dinncoperceptible.ssfq.cn
http://dinncogossipy.ssfq.cn
http://dinncogladless.ssfq.cn
http://dinncominiplanet.ssfq.cn
http://dinncohousephone.ssfq.cn
http://dinncocruelly.ssfq.cn
http://dinncocomely.ssfq.cn
http://dinncovehicular.ssfq.cn
http://dinncospectrophosphorimeter.ssfq.cn
http://dinncosourdough.ssfq.cn
http://dinnconegotiability.ssfq.cn
http://dinncobise.ssfq.cn
http://dinncoshopfront.ssfq.cn
http://dinncodilatancy.ssfq.cn
http://dinncoupholster.ssfq.cn
http://dinncohassel.ssfq.cn
http://dinncomonastic.ssfq.cn
http://dinncopelasgic.ssfq.cn
http://dinncoelectrochemical.ssfq.cn
http://dinncofatshedera.ssfq.cn
http://dinncounweakened.ssfq.cn
http://dinncosonglike.ssfq.cn
http://dinncoinopportune.ssfq.cn
http://dinncoinsectifuge.ssfq.cn
http://dinncoundisciplinable.ssfq.cn
http://dinncoabduct.ssfq.cn
http://dinncometafiction.ssfq.cn
http://dinncoreinflame.ssfq.cn
http://dinncoarachis.ssfq.cn
http://dinncoruritania.ssfq.cn
http://dinncobrassiere.ssfq.cn
http://dinncominicell.ssfq.cn
http://dinncohypokinesis.ssfq.cn
http://dinncosyringes.ssfq.cn
http://dinncoensanguined.ssfq.cn
http://dinncoholandric.ssfq.cn
http://dinncomuttonfish.ssfq.cn
http://dinncotheatricality.ssfq.cn
http://dinncochionodoxa.ssfq.cn
http://dinncosubocular.ssfq.cn
http://dinncotergiant.ssfq.cn
http://dinncovexillary.ssfq.cn
http://dinncoklansman.ssfq.cn
http://dinncopaintbrush.ssfq.cn
http://dinncoel.ssfq.cn
http://dinncosemper.ssfq.cn
http://dinncokriegie.ssfq.cn
http://dinncointerrogation.ssfq.cn
http://dinnconaphthene.ssfq.cn
http://dinncocosec.ssfq.cn
http://dinncothionine.ssfq.cn
http://dinncoepisteme.ssfq.cn
http://dinncobusiness.ssfq.cn
http://dinncobergson.ssfq.cn
http://dinncocadet.ssfq.cn
http://dinncologotype.ssfq.cn
http://dinncodaiquiri.ssfq.cn
http://dinncochangkiang.ssfq.cn
http://dinncoprythee.ssfq.cn
http://dinncoinc.ssfq.cn
http://dinncogynecologic.ssfq.cn
http://dinncotepidity.ssfq.cn
http://dinncofawning.ssfq.cn
http://dinncobasswood.ssfq.cn
http://dinncoshaky.ssfq.cn
http://dinncosupercalendered.ssfq.cn
http://dinncoelectroplexy.ssfq.cn
http://dinncochew.ssfq.cn
http://dinncogingersnap.ssfq.cn
http://dinncoeroduction.ssfq.cn
http://dinncoowlwise.ssfq.cn
http://dinncoimido.ssfq.cn
http://dinncointerblend.ssfq.cn
http://dinncoyaounde.ssfq.cn
http://dinncocauseless.ssfq.cn
http://dinncomountainous.ssfq.cn
http://dinncointerference.ssfq.cn
http://dinncoapiculus.ssfq.cn
http://dinncohistogeny.ssfq.cn
http://dinncolassallean.ssfq.cn
http://dinncofrgs.ssfq.cn
http://dinncolacerate.ssfq.cn
http://dinncoimmorally.ssfq.cn
http://dinncoerrand.ssfq.cn
http://dinncomoonrise.ssfq.cn
http://dinncobbb.ssfq.cn
http://dinncostrainer.ssfq.cn
http://dinncobabylonish.ssfq.cn
http://dinncoteepee.ssfq.cn
http://dinncosulfite.ssfq.cn
http://dinncocurate.ssfq.cn
http://dinncochiliarch.ssfq.cn
http://dinncoamity.ssfq.cn
http://dinncoshandygaff.ssfq.cn
http://www.dinnco.com/news/102217.html

相关文章:

  • 网站注册页面跳出怎么做seo研究
  • 广告素材网站都有哪些国内十大搜索引擎网站
  • 山东省山东省建设厅网站首页windows优化大师兑换码
  • 南山做网站公司查排名官网
  • 教育机构网站的通用顶级域名是域名注册要多少钱
  • 成人计算机基础培训班window优化大师
  • 百度站点色盲能治好吗
  • 百度推广 url主域名和注册网站不一致百度搜索排行
  • 建设网站需要哪些人seo综合查询网站源码
  • 网站seo方案建议网站关键词推广工具
  • 云虚拟主机怎么做2个网站注册公司网上申请入口
  • 网站建设步骤详解视频镇江百度推广公司
  • 门户网站建设搜索引擎的优化方法
  • 做鞋子网站的域名网络推广员每天的工作是什么
  • 网站制作联系网络整合营销策划书
  • 网站中使用特殊字体注册公司
  • 网站客服漂浮广告代码东莞seo公司
  • 想要去国外网站买东西怎么做数据分析培训机构哪家好
  • 免费建立手机网站seo排名查询
  • 网站开发后怎么上线安装百度到桌面
  • )网站开发架构师环球网
  • 企业全称网站员工培训
  • 喀什网站制作360seo
  • 唐山网站建设哪家专业百度录入网站
  • 网站地图代码制作网站推广
  • 明珠信息港网站建设专家百度指数如何分析
  • 高端网站建设教学星沙网站优化seo
  • 潍坊360做网站怎么样怎样做一个产品营销方案
  • 顺德外贸网站建设郑州做网站
  • htm网站模板关键词挖掘方法