算法训练营第二十一天 | LeetCode 513 找树左下角的值、LeetCode 112 路径总和、LeetCode 106 从中序与后序遍历构造二叉树

ops/2024/9/23 20:20:13/

LeetCode 513 找树左下角的值

这题要求找树最底层最左边节点的值,用单纯的迭代法、递归法都不太好处理,一般的层序遍历法也不太行。但是可以修改下层序遍历,改成每一层从右往左遍历,依次往下,这样子访问的最后一个节点就是最底层最左边的节点了。这也用到了广度优先搜索的方法。

代码如下:

class Solution {
public:int findBottomLeftValue(TreeNode* root) {queue<TreeNode*> myque;TreeNode* cur = root;myque.push(root);while (!myque.empty()) {int num = myque.size();while (num--) {cur = myque.front();myque.pop();if (cur->right) myque.push(cur->right);if (cur->left) myque.push(cur->left);}}return cur->val;}
};

深度优先搜索是递归的改版。之后有时间再写下。

LeetCode 112 路径总和

和昨天一样的回溯法

class Solution {
private:int num = 0;int sum = 0;
public:void backtracking(TreeNode* cur, int targetSum) {if (!cur->left && !cur->right) {if (sum == targetSum) num++;return;}if (cur->left) {sum += cur->left->val;backtracking(cur->left, targetSum);sum -= cur->left->val;}if (cur->right) {sum += cur->right->val;backtracking(cur->right, targetSum);sum -= cur->right->val;}}bool hasPathSum(TreeNode* root, int targetSum) {if (!root) return false;targetSum -= root->val;backtracking(root, targetSum);return (bool)num;}
};

也可以像求高度一样用递归。这里就不写了。

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

这题感觉确实是有难度了。首先是对于中序遍历和后序遍历确定一棵树的理解,其次是递归时递归逻辑的选择和确定。还可以用map降低时间复杂度,以及要注意先递归构建右子树,再递归构建左子树,这样方便从后往前地从后序遍历中取数作为根节点查找依据。之所以这样也是因为迭代法求后序遍历时所收获的一些感悟和心得——后续遍历是中右左遍历方式的反转,每次放入的节点一定先是局部状态下的根节点,后是局部状态下的右节点,再是局部状态下的左节点。

代码如下:

class Solution {
public:TreeNode* mybuildTree(vector<int>& inorder, int in_l, int in_r, vector<int>& postorder, int post_l, int post_r) {if (in_l > in_r) return nullptr;// if (in_l == in_r) {//     TreeNode* newNode = new TreeNode(inorder[in_l]);//     return newNode;// }int rootval = postorder[post_r];int in_rootIndex = 0;for (int i = in_l; i <= in_r; i++) {if (inorder[i] == rootval) {in_rootIndex = i;break;}}TreeNode* root = new TreeNode(rootval);if (in_rootIndex > in_l) {root->left = mybuildTree(inorder, in_l, in_rootIndex -1, postorder, post_l, post_l + (in_rootIndex - 1-in_l));}if (in_r > in_rootIndex) {int first_r = inorder[in_rootIndex + 1];int post_rstart = 0;for (int i = post_l; i <= post_r; i++) {if (postorder[i] == first_r) {post_rstart = i;break;}}root->right = mybuildTree(inorder, in_rootIndex + 1, in_r, postorder, post_rstart, post_r - 1);}return root;}TreeNode* buildTree(vector<int>& inorder, vector<int>& postorder) {return mybuildTree(inorder, 0, inorder.size() - 1, postorder, 0, inorder.size() - 1);}
};

中间四行不注释掉会报错。


http://www.ppmy.cn/ops/37195.html

相关文章

layui的treeTable组件,多层级上传按钮失效的问题解决

现象描述: layui的treeTable 的上传按钮在一层能用&#xff0c;展开后其他按钮正常点击&#xff0c;上传按钮无效。 具体原因没有深究&#xff0c;大概率是展开的子菜单没有被渲染treeTable的done管理到&#xff0c;导致没有重绘上传按钮。 解决方案: 不使用layu的上传组件方法…

【C++】string类的模拟实现

一、经典的string类问题 上一期&#xff0c;我们已经对string类有了简单的了解&#xff0c;大家能正常使用即可。在面试中&#xff0c;面试官喜欢让同学们自己来模拟实现string类&#xff0c;主要是实现string类的构造、拷贝构造、赋值运算符重载以及析构函数。请看以下string…

Nginx配置多个前端项目

1、修改nginx.conf配置文件&#xff1b; 2、必须包含默认的跟路径 location / { root D:/work/nginx-1.22.0/html; index index.html; } 3、添加要访问的前端项目信息&#xff0c;必须使用alias而不能使用root location /…

搭建Docker私有镜像仓库

大家好&#xff0c;今天给大家分享一下如何搭建私有镜像仓库&#xff0c;私有镜像仓库可以更好地管理和控制镜像的访问和使用&#xff0c;确保只有授权的人员能够获取和使用特定的镜像&#xff0c;而且方便团队内部共享定制化的镜像&#xff0c;提高开发和部署效率&#xff0c;…

人大金仓V8R6迁移mysql8.0

人大金仓数据库迁移mysql mysql版本&#xff1a;mysql 8.0.22 人大金仓版本;KingbaseES V008R006C008B0014 on x64 打开数据迁移工具 等待执行完成后使用命令窗口中提示的地址在浏览器中打开&#xff1a; 登录。此处登录不用修改任何信息&#xff0c;点击登录即可 新建源数…

数据结构-线性表-应用题-2.2-14

1&#xff09;算法基本设计思想&#xff1a; 2&#xff09;c语言描述&#xff1a; #define INT_MAX 0X7FFFFFFF int abs_(int a) {//绝对值if(a<0) return -a;else return a; } bool min(int a,int b,int c){if(a<b&&a<c) return true;else return false; } …

iOS 10权限问题

简单说明 1.注意需要打开info.plist文件添加相应权限以及权限的说明&#xff0c;否则程序在iOS10上会出现崩溃。 2.且添加时注意不要有空格。 3.输入Privacy一般会有提示。 权限说明 iOS 10支持的所有权限类型 Privacy - Bluetooth Peripheral Usage Description 蓝牙权限…

内容安全(IPS入侵检测)

入侵检测系统&#xff08; IDS &#xff09;---- 网络摄像头&#xff0c;侧重于风险管理&#xff0c;存在于滞后性&#xff0c;只能够进行风险发现&#xff0c;不能及时制止。而且早期的IDS误报率较高。优点则是可以多点进行部署&#xff0c;比较灵活&#xff0c;在网络中可以进…