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

郑州网站优化的微博_腾讯微博乐陵seo优化信

郑州网站优化的微博_腾讯微博,乐陵seo优化信,网站添加js广告位,广州市建筑工程有限公司代码随想录二刷Day18 今日任务 513.找树左下角的值 112.路径总和 113.路径总和ii 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树 语言#xff1a;C 513.找树左下角的值 链接#xff1a;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) {queueTreeNode* 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){stackTreeNode* 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:vectorvectorint res;vectorint 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();}}vectorvectorint pathSum(TreeNode* root, int targetSum) {if(root NULL) return res;traversal(root, targetSum);return res;} };迭代 class Solution { public:vectorvectorint res;vectorint path;int curSum 0;vectorvectorint pathSum(TreeNode* root, int targetSum) {if(root NULL) return res;stackTreeNode* 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(vectorint inorder, vectorint 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);vectorint left_in(inorder.begin(), inorder.begin() i);vectorint left_post(postorder.begin(), postorder.begin() i);root-left traversal(left_in, left_post);vectorint right_in(inorder.begin() i 1, inorder.end());vectorint right_post(postorder.begin() i, postorder.end());root-right traversal(right_in, right_post);return root;}TreeNode* buildTree(vectorint inorder, vectorint postorder) {return traversal(inorder, postorder);} };105.从前序与中序遍历序列构造二叉树 链接https://leetcode.cn/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ 递归 class Solution { public:TreeNode* traversal(vectorint preorder, vectorint 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;}vectorint left_pre(preorder.begin() 1, preorder.begin() 1 i);vectorint left_in(inorder.begin(), inorder.begin() i);root-left traversal(left_pre, left_in);vectorint right_pre(preorder.begin() 1 i, preorder.end());vectorint right_in(inorder.begin() i 1, inorder.end());root-right traversal(right_pre, right_in);return root;}TreeNode* buildTree(vectorint preorder, vectorint inorder) {return traversal(preorder, inorder);} };
http://www.w-s-a.com/news/349728/

相关文章:

  • vr超市门户网站建设班级网站怎么做ppt模板
  • 网站建设一般是用哪个软件刚开始做写手上什么网站
  • 用jsp做的网站源代码下载有哪些做红色旅游景点的网站
  • 网站开发的技术选型黄石市网站建设
  • 做直播网站需要证书吗专做宝宝的用品网站
  • 网站标题用什么符号网站制作交易流程
  • dede模板网站教程jsp网站搭建
  • 上海网站开发外包公司鲜花导购网页制作
  • 宿州外贸网站建设公司个人注册网站一般做什么
  • 小公司做网站用哪种服务器什么是网站代理
  • 青岛李村网站设计公司cms建站平台
  • 做saas网站可行吗许昌抖音推广公司
  • 网站建设找谁做seo基础知识培训
  • 微网站怎么做的好建设网站不会写代码
  • 广州外贸网站制作wordpress信息搜索插件
  • 福建高端网站建设个人公众号怎么制作教程
  • 企业网站有哪些举几个例子wordpress ie兼容插件
  • 高端的深圳网站页面设计福清市建设局官方网站
  • 安装网站到服务器合肥建设干部学校网站
  • 影视网站如何做销售案例网站
  • 建设网站对比方案龙岗网站开发公司
  • 网站开发标准网站建设公司兴田德润可信赖
  • 如何建设一个公众号电影网站自动seo优化
  • 个人网站能备案吗酱香拿铁采取了哪些网络营销方式
  • 网站建设及推广好做吗自己做的网站加入购物车价格
  • 涡阳在北京做网站的名人注册一个免费的网站
  • 三门峡建设环境局网站公司注册网上核名通道
  • 叶县建设局网站要看网海外域名是多少
  • 网站运行环境配置Wordpress支付时效
  • logo设计网站知乎港北网站建设