513. 找树左下角的值

即找最后一行的最左侧

//递归法,前序
class Solution {
public:int maxDepth = INT_MIN;int result;void traversal(TreeNode* root, int depth) {if (root->left == NULL && root->right == NULL) {if (depth > maxDepth) {maxDepth = depth;//最大深度result = root->val;}return;}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;}
};
//层次迭代
class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> que;if (root != NULL) que.push(root);int result = 0;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;}
};

112. 路径总和

记录总和,遍历去减

//前序递归
class Solution {
private:bool traversal(TreeNode* cur, int count) {if (!cur->left && !cur->right && count == 0) return true; // 遇到叶子节点,并且计数为0if (!cur->left && !cur->right) return false; // 遇到叶子节点直接返回,计数不为0if (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);}
};
//化简
class Solution {
public:bool hasPathSum(TreeNode* root, int sum) {if (!root) return false;if (!root->left && !root->right && sum == root->val) {return true;}return hasPathSum(root->left, sum - root->val) || hasPathSum(root->right, sum - root->val);}
};
//迭代
class Solution {
public:bool hasPathSum(TreeNode* root, int targetSum) {if(root==nullptr)return false;//栈存放pair<节点指针,路径数值>stack<pair<TreeNode*,int>> st;st.push(pair<TreeNode*, int>(root, root->val));while (!st.empty()) {pair<TreeNode*, int> node = st.top();st.pop();// 如果该节点是叶子节点了,同时该节点的路径数值等于sum,那么就返回trueif (!node.first->left && !node.first->right && targetSum == node.second) return true;// 右节点,压进去一个节点的时候,将该节点的路径数值也记录下来if (node.first->right) {st.push(pair<TreeNode*, int>(node.first->right, node.second + node.first->right->val));}// 左节点,压进去一个节点的时候,将该节点的路径数值也记录下来if (node.first->left) {st.push(pair<TreeNode*, int>(node.first->left, node.second + node.first->left->val));}}return false;}
};

113. 路径总和 II

递归法

class Solution {
private:vector<vector<int>> result;vector<int> path;// 递归函数不需要返回值,因为我们要遍历整个树void traversal(TreeNode* cur, int count) {if (!cur->left && !cur->right && count == 0) { // 遇到了叶子节点且找到了和为sum的路径result.push_back(path);return;}if (!cur->left && !cur->right) return ; // 遇到叶子节点而没有找到合适的边,直接返回if (cur->left) { // 左 (空节点不遍历)path.push_back(cur->left->val);count -= cur->left->val;traversal(cur->left, count);    // 递归count += cur->left->val;        // 回溯path.pop_back();                // 回溯}if (cur->right) { // 右 (空节点不遍历)path.push_back(cur->right->val);count -= cur->right->val;traversal(cur->right, count);   // 递归count += cur->right->val;       // 回溯path.pop_back();                // 回溯}return ;}
public: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;}
};

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

后序遍历分割中序左右

class Solution {
private: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 delimiterIndex;for (delimiterIndex = 0; delimiterIndex < inorder.size(); delimiterIndex++) {if (inorder[delimiterIndex] == rootValue) break;}// 切割中序数组// 左闭右开区间:[0, delimiterIndex)vector<int> leftInorder(inorder.begin(), inorder.begin() + delimiterIndex);// [delimiterIndex + 1, end)vector<int> rightInorder(inorder.begin() + delimiterIndex + 1, inorder.end() );// postorder 舍弃末尾元素postorder.resize(postorder.size() - 1);// 切割后序数组// 依然左闭右开,注意这里使用了左中序数组大小作为切割点// [0, leftInorder.size)vector<int> leftPostorder(postorder.begin(), postorder.begin() + leftInorder.size());// [leftInorder.size(), end)vector<int> rightPostorder(postorder.begin() + leftInorder.size(), postorder.end());root->left = traversal(leftInorder, leftPostorder);root->right = traversal(rightInorder, rightPostorder);return root;}
public:TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {if (inorder.size() == 0 || postorder.size() == 0) return NULL;return traversal(inorder, postorder);}
};
class Solution {
private:// 中序区间:[inorderBegin, inorderEnd),后序区间[postorderBegin, postorderEnd)TreeNode* traversal (vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& postorder, int postorderBegin, int postorderEnd) {if (postorderBegin == postorderEnd) return NULL;int rootValue = postorder[postorderEnd - 1];TreeNode* root = new TreeNode(rootValue);if (postorderEnd - postorderBegin == 1) return root;int delimiterIndex;for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {if (inorder[delimiterIndex] == rootValue) break;}// 切割中序数组// 左中序区间,左闭右开[leftInorderBegin, leftInorderEnd)int leftInorderBegin = inorderBegin;int leftInorderEnd = delimiterIndex;// 右中序区间,左闭右开[rightInorderBegin, rightInorderEnd)int rightInorderBegin = delimiterIndex + 1;int rightInorderEnd = inorderEnd;// 切割后序数组// 左后序区间,左闭右开[leftPostorderBegin, leftPostorderEnd)int leftPostorderBegin =  postorderBegin;int leftPostorderEnd = postorderBegin + delimiterIndex - inorderBegin; // 终止位置是 需要加上 中序区间的大小size// 右后序区间,左闭右开[rightPostorderBegin, rightPostorderEnd)int rightPostorderBegin = postorderBegin + (delimiterIndex - inorderBegin);int rightPostorderEnd = postorderEnd - 1; // 排除最后一个元素,已经作为节点了root->left = traversal(inorder, leftInorderBegin, leftInorderEnd,  postorder, leftPostorderBegin, leftPostorderEnd);root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, postorder, rightPostorderBegin, rightPostorderEnd);return root;}
public:TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {if (inorder.size() == 0 || postorder.size() == 0) return NULL;// 左闭右开的原则return traversal(inorder, 0, inorder.size(), postorder, 0, postorder.size());}
};

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

class Solution {
private:TreeNode* traversal (vector<int>& inorder, int inorderBegin, int inorderEnd, vector<int>& preorder, int preorderBegin, int preorderEnd) {if (preorderBegin == preorderEnd) return NULL;int rootValue = preorder[preorderBegin]; // 注意用preorderBegin 不要用0TreeNode* root = new TreeNode(rootValue);if (preorderEnd - preorderBegin == 1) return root;int delimiterIndex;for (delimiterIndex = inorderBegin; delimiterIndex < inorderEnd; delimiterIndex++) {if (inorder[delimiterIndex] == rootValue) break;}// 切割中序数组// 中序左区间,左闭右开[leftInorderBegin, leftInorderEnd)int leftInorderBegin = inorderBegin;int leftInorderEnd = delimiterIndex;// 中序右区间,左闭右开[rightInorderBegin, rightInorderEnd)int rightInorderBegin = delimiterIndex + 1;int rightInorderEnd = inorderEnd;// 切割前序数组// 前序左区间,左闭右开[leftPreorderBegin, leftPreorderEnd)int leftPreorderBegin =  preorderBegin + 1;int leftPreorderEnd = preorderBegin + 1 + delimiterIndex - inorderBegin; // 终止位置是起始位置加上中序左区间的大小size// 前序右区间, 左闭右开[rightPreorderBegin, rightPreorderEnd)int rightPreorderBegin = preorderBegin + 1 + (delimiterIndex - inorderBegin);int rightPreorderEnd = preorderEnd;root->left = traversal(inorder, leftInorderBegin, leftInorderEnd,  preorder, leftPreorderBegin, leftPreorderEnd);root->right = traversal(inorder, rightInorderBegin, rightInorderEnd, preorder, rightPreorderBegin, rightPreorderEnd);return root;}public:TreeNode* buildTree(vector<int>& preorder, vector<int>& inorder) {if (inorder.size() == 0 || preorder.size() == 0) return NULL;// 参数坚持左闭右开的原则return traversal(inorder, 0, inorder.size(), preorder, 0, preorder.size());}
};

代码随想录十八天|513,112,113,106,105相关推荐

  1. 代码随想录第17天|513,112,113,106,105

    文章目录 513.找树左下角的值 112. 路径总和 113.路径总和ii 106.从中序与后序遍历序列构造二叉树 105.从前序与中序遍历序列构造二叉树 513.找树左下角的值 本题要找出树的最后一 ...

  2. _28LeetCode代码随想录算法训练营第二十八天-贪心算法 | 122.买卖股票的最佳时机II 、55.跳跃游戏、45.跳跃游戏II

    _28LeetCode代码随想录算法训练营第二十八天-贪心算法 | 122.买卖股票的最佳时机II .55.跳跃游戏.45.跳跃游戏II 题目列表 122.买卖股票的最佳时机II 55.跳跃游戏 45 ...

  3. 代码随想录一刷个人记录

    代码随想录一刷个人记录 2022/7/14(leetcode) 2022/7/15(leetcode) 2022/7/17[字符串] 2022/7/18[字符串] 2022/7/20[双指针] 202 ...

  4. 代码随想录之路经总和

    路径总和力扣112 路径总和|| 力扣113 本题的思路学习于代码随想录 JAVA版本 一.路径总和: 本题的思路较为简单,只需要用target来减去值即可. 解法一:使用递归的方法 class So ...

  5. 代码随想录算法训练营第二天 | 力扣977.有序数组的平方,209.长度最小的子数组,59.螺旋矩阵II

    代码随想录算法训练营第二天 | 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II 977.有序数组的平方 题目链接:有序数组的平方 题目描述: 给你一个按 非递减顺序 排序的整 ...

  6. 代码随想录(day04)-LeetCode:24、19、面试题02.07、142

    代码随想录:dayo4 1. [24]两两交换链表中的节点 虚拟头结点实现 递归实现 2.[19]**删除链表的倒数第N个节点** 双指针算法 3. 面试题[02.07]:链表相交 4.[142]环形 ...

  7. 代码随想录【day 14 二叉树】| 层序遍历 226.翻转二叉树 101.对称二叉树

    代码随想录[day 14 二叉树]| 层序遍历 226.翻转二叉树 101.对称二叉树 层序遍历 卡哥文解 视频讲解 题目链接:102.二叉树的层序遍历 解题思路 代码实现 题目链接:107.二叉树的 ...

  8. 墙裂推荐!卡神力作《代码随想录》,上架首日卖爆!

    刚开始学习数据结构与算法,或者在力扣(LeetCode)上刷题的读者都有这种困惑--从何学起,先学什么,再学什么.很多人刷题的效率低,主要体现在以下三点: 难以寻找适合自己的题目. 找到了不合适现阶段 ...

  9. 送卡神算法力作《代码随想录》!

    刚开始学习数据结构与算法,或者在力扣(LeetCode)上刷题的读者都有这种困惑--从何学起,先学什么,再学什么.很多人刷题的效率低,主要体现在以下三点: 难以寻找适合自己的题目. 找到了不合适现阶段 ...

最新文章

  1. cp: /usr/bin/chromedriver: Operation not permitted
  2. 行业软件和鸿蒙,华为鸿蒙负责人王成录:育人才,打造国产软件“根”能力
  3. 面向对象中的session版的购物车
  4. navicat导出数据到oracle,使用Navicat premium导出oracle数据库中数据到SQL server2008数据库中...
  5. cobol_在尝试之前不要讨厌COBOL
  6. C#.Net工作笔记007---关于Lst深层复制_浅层复制_提供一个方法可以直接使用
  7. 【转】利用Eclipse编辑中文资源文件(application_zh_CN.properties )
  8. The Turn Model for Adaptive Routing中的west-first算法
  9. Django之路第四篇:Models
  10. C++_class Template about Stack(使用类模板实现栈操作)
  11. excel表格乱码修复_修复从数据库复制的空白Excel单元格
  12. php社工源码,社工库源码搜集
  13. H5调用app原生接口
  14. mysql secure_file_priv 属性相关的文件读写权限问题
  15. Java并发编程之CyclicBarrier和CountDownLatch
  16. 笔记本电脑 a disk read error occurred 问题解决
  17. 程序员初创公司的合伙人股权的进入和退出机制设计-20151020
  18. 电脑本地连接图标不见 怎样找到恢复处理无法创建宽带连接(转)
  19. docker网络模式与资源控制
  20. linux 启动脚本

热门文章

  1. SMA TE: Semi-Supervised Spatio-Temporal RepresentationLearning on Multivariate Time Series
  2. 下载付费音乐(贝塔的随手日记)
  3. 荒野无灯路由器固件配置DDNS的过程
  4. 数据拟合算法c语言实现,数据拟合算法剖析及C语言实现.doc
  5. c语言读取pnm图片,ppm图像相关 - osc_p1rj1z8j的个人空间 - OSCHINA - 中文开源技术交流社区...
  6. python语言的未来前景-Python未来前景怎么样?
  7. 新能源——插混、油混、增程
  8. 隐私计算--23--纵向联邦学习
  9. C#为图片添加水印,生成缩略图
  10. 40+ 新鲜漂亮的大背景网站设计