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

多图片网站优化百度灰色关键词排名

多图片网站优化,百度灰色关键词排名,济南好的网站建设公司哪家好,绍兴网站建设公司哪家专业代码随想录二刷Day18 今日任务 513.找树左下角的值 112.路径总和 113.路径总和ii 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树 语言:C 513.找树左下角的值 链接:https://leetcode.cn/problems/find-bottom-left-tree-va…

代码随想录二刷Day18

今日任务

513.找树左下角的值
112.路径总和
113.路径总和ii
106.从中序与后序遍历序列构造二叉树
105.从前序与中序遍历序列构造二叉树
语言:C++

513.找树左下角的值

链接:https://leetcode.cn/problems/find-bottom-left-tree-value/
递归

class Solution {
public:int maxDepth = INT_MIN; //这里要用负数,避免树只有一层结构时无法更新resint res = 0;void traversal(TreeNode* root, int depth){if(root == NULL) return;if(root->left == NULL && root->right == NULL){if(depth > maxDepth){ //保证是最左边的元素,如果是同一层元素的话,不会更新maxDepth和res的值maxDepth = depth;res = root->val;}return;}if(root->left){traversal(root->left, depth + 1);}if(root->right){traversal(root->right, depth + 1);}}int findBottomLeftValue(TreeNode* root) {traversal(root, 0);return res;}
};

迭代

class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> que;que.push(root);int res = 0;while(!que.empty()){int n = que.size();for(int i = 0; i < n; i++){TreeNode* cur = que.front();que.pop();if(i == 0){res = cur->val;}if(cur->left) que.push(cur->left);if(cur->right) que.push(cur->right);}}return res;}
};

112.路径总和

链接:https://leetcode.cn/problems/path-sum/
递归

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

迭代

class Solution {
public:int curSum = 0;bool traversal(TreeNode* root, int targetSum){stack<TreeNode*> st;st.push(root);while(!st.empty()){TreeNode* cur = st.top();if(cur != NULL){st.push(NULL);curSum += cur->val; //中if(cur->left == NULL && cur->right == NULL && curSum == targetSum) return true;if(cur->right) st.push(cur->right); //右if(cur->left) st.push(cur->left); //左}else{st.pop(); //NULLcur = st.top();st.pop();curSum -= cur->val;}}return false;}bool hasPathSum(TreeNode* root, int targetSum) {if(root == NULL) return false;return traversal(root, targetSum);}
};

113.路径总和ii

链接:https://leetcode.cn/problems/path-sum-ii/
递归

class Solution {
public:vector<vector<int>> res;vector<int> path;void traversal(TreeNode* root, int target){if(root == NULL) return;if(root->left == NULL && root->right == NULL && target != root->val) return;if(root->left == NULL && root->right == NULL && target == root->val){path.push_back(root->val);res.push_back(path);path.pop_back(); //回溯return;}if(root->left){path.push_back(root->val);traversal(root->left, target - root->val);path.pop_back();}if(root->right){path.push_back(root->val);traversal(root->right, target - root->val);path.pop_back();}}vector<vector<int>> pathSum(TreeNode* root, int targetSum) {if(root == NULL) return res;traversal(root, targetSum);return res;}
};

迭代

class Solution {
public:vector<vector<int>> res;vector<int> path;int curSum = 0;vector<vector<int>> pathSum(TreeNode* root, int targetSum) {if(root == NULL) return res;stack<TreeNode*> st;st.push(root);while(!st.empty()){TreeNode* cur = st.top();if(cur != NULL){st.push(NULL);curSum += cur->val;path.push_back(cur->val);if(cur->left == NULL && cur->right == NULL && curSum == targetSum){res.push_back(path);}if(cur->right) st.push(cur->right);if(cur->left) st.push(cur->left);}else{st.pop();cur = st.top();st.pop();curSum -= cur->val;path.pop_back();}}return res;}
};

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

链接:https://leetcode.cn/problems/construct-binary-tree-from-inorder-and-postorder-traversal/
递归

class Solution {
public:TreeNode* traversal(vector<int>& inorder, vector<int>& postorder){if(postorder.size() == 0) return NULL;int val = postorder[postorder.size() - 1];TreeNode* root = new TreeNode(val);if(postorder.size() == 1) return root;int i;for(i = 0; i < inorder.size(); i++){if(inorder[i] == val) break;}postorder.resize(postorder.size() - 1);vector<int> left_in(inorder.begin(), inorder.begin() + i);vector<int> left_post(postorder.begin(), postorder.begin() + i);root->left = traversal(left_in, left_post);vector<int> right_in(inorder.begin() + i + 1, inorder.end());vector<int> right_post(postorder.begin() + i, postorder.end());root->right = traversal(right_in, right_post);return root;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {return traversal(inorder, postorder);}
};

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

链接:https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/
递归

class Solution {
public:TreeNode* traversal(vector<int>& preorder, vector<int>& inorder){if(inorder.size() == 0) return NULL;int val = preorder[0];TreeNode* root = new TreeNode(val);if(inorder.size() == 1) return root;int i;for(i = 0; i < inorder.size(); i++){if(inorder[i] == val) break;}vector<int> left_pre(preorder.begin() + 1, preorder.begin() + 1 + i);vector<int> left_in(inorder.begin(), inorder.begin() + i);root->left = traversal(left_pre, left_in);vector<int> right_pre(preorder.begin() + 1 + i, preorder.end());vector<int> right_in(inorder.begin() + i + 1, inorder.end());root->right = traversal(right_pre, right_in);return root;}TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {return traversal(preorder, inorder);}
};
http://www.fp688.cn/news/141667.html

相关文章:

  • 招代理网站建设公司网络推广的好处
  • 我自己的网站怎么做关键词优化优化关键词排名seo
  • 开发一个直播平台需要多少钱常州seo建站
  • 河池城乡住房和建设局网站信阳搜索引擎优化
  • 用国外服务器做赌博网站超级seo助手
  • 南山网站建设深圳信科今日热点新闻事件简介
  • 东莞网站建设三合一营销策划方案范文1500
  • 郑州公司企业网站建设外贸网站营销推广
  • 尚层装饰快速优化官网
  • 做AMC12的题的网站网站服务费一年多少钱
  • 加强网站 网站建设陕西网站推广公司
  • 做赌博我网站找第三方支付宁波seo费用
  • 哪个网站做图文素材多站长统计网站大全
  • 免费网站虚拟主机seo全网营销
  • 网站前端提成多少网络营销师证书含金量
  • 郑州网站建设及优化优化seo
  • 国办政府网站建设规范搜索引擎收录查询
  • 旅游网站 系统最有创意的广告语30条
  • 网站建设分金手指专业二八网页快速收录
  • wordpress移动版无法正常显示江苏seo技术教程
  • 网站建设公司名字b2b关键词排名工具
  • 一个用户注册的网站怎么做seo关键词优化要多少钱
  • 广东新增本土确诊病例北京网站优化快速排名
  • 广东app开发公司排行榜seo短视频
  • 大连做网站需要多少钱培训机构网站
  • 在哪个网站做任务赚钱优化网站页面
  • 视频网站的建设无线网络优化
  • 个人做网站做什么样的话国内做网站的公司
  • 澳门网站维护骗局汕头百度网站排名
  • 宝鸡门户网站开发seo网站推广优化