day18-二叉树统一迭代写法

chatgpt/2023/10/4 8:03:06

二叉树统一迭代写法

思路

前面说了,前序/后序和中序不能统一的原因是因为,访问和处理的时机不同,如果要做到同时处理,我们可以在要处理的结点后放一个空指针作为标记。如下

前序代码:

class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> st;if(!root)return res;st.push(root);while(!st.empty()){TreeNode* cur = st.top();if(cur != nullptr){st.pop();if(cur->right) st.push(cur->right);if(cur->left) st.push(cur->left);st.push(cur);st.push(nullptr);}else{st.pop();TreeNode* node = st.top();st.pop();res.push_back(node->val);}}return res;}
};

中序代码:

class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> st;if(!root)return res;st.push(root);while(!st.empty()){TreeNode* cur = st.top();if(cur != nullptr){st.pop();if(cur->right) st.push(cur->right);st.push(cur);st.push(nullptr);if(cur->left) st.push(cur->left);}else{st.pop();TreeNode* node = st.top();st.pop();res.push_back(node->val);}}return res;}
};

后序代码:

class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> res;stack<TreeNode*> st;if(root == nullptr)return res;st.push(root);while(!st.empty()){TreeNode* cur = st.top();if(cur!=nullptr){st.pop();st.push(cur); // 中st.push(nullptr);if(cur->right) st.push(cur->right);if(cur->left) st.push(cur->left);}else{st.pop();TreeNode* node = st.top();st.pop();res.push_back(node->val);}}return res;}
};

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.exyb.cn/news/show-5313823.html

如若内容造成侵权/违法违规/事实不符,请联系郑州代理记账网进行投诉反馈,一经查实,立即删除!

相关文章

OpenVDB Volume Render OverView

OpenVDB & Volume Render OverView OpenVDB OpenVDB is an open source C library comprising a novel hierarchical data structure and a large suite of tools for the efficient storage and manipulation of sparse volumetric data discretized on three-dimensio

【黑马头条之redis实现延迟任务】

本笔记内容为黑马头条项目的延迟任务精准发布文章部分 目录 一、实现思路 二、延迟任务服务实现 1、搭建heima-leadnews-schedule模块 2、数据库准备 3、安装redis 4、项目集成redis 5、添加任务 6、取消任务 7、消费任务 8、未来数据定时刷新 1.reids key值匹配 …

golang使用泛型实现mapreduce操作

1.使用面向对象的方式写 package streamimport ("fmt""log""reflect""sort""strconv""strings" )type Stream[T any] struct {data []TkeyBy stringsortByNum stringsortByStr []string }func FromElem…

UE C++ 知识补充

反射 描述运行时得到类型的功能&#xff0c;通过类型信息反过来创建对象&#xff0c;读取修改属性&#xff0c;调用方法的功能行为 反射用于在是在程序运行时动态加载类以及获取类的信息&#xff0c;反射数据描述了类在运行时的内容这些数据所存储的信息包括类的名称、类中的数…

# Unity 如何获取Texture 的内存大小

Unity 如何获取Texture 的内存大小 在Unity中&#xff0c;要获取Texture的内存文件大小&#xff0c;可以使用UnityEditor.TextureUtil类中的一些函数。这些函数提供了获取存储内存大小和运行时内存大小的方法。由于UnityEditor.TextureUtil是一个内部类&#xff0c;我们需要使…

【Git系列】Git配置SSH免密登录

&#x1f433;Git配置SSH免密登录 &#x1f9ca;1.设置用户名和邮箱&#x1f9ca;2. 生成密钥&#x1f9ca;3.远程仓库配置密钥&#x1f9ca;2. 免密登录 在以上push操作过程中&#xff0c;我们第一次push时&#xff0c;是需要进行录入用户名和密码的&#xff0c;比较麻烦。而且…

使用深度学习模型对视频进行聚类分析-Pytorch、Skleran、Matplotlib

from sklearn.datasets import make_circles from sklearn.cluster import KMeans, DBSCAN, SpectralClustering, Birch, MeanShift, AgglomerativeClustering from sklearn.metrics import silhouette_score, silhouette_samples from sklearn.decomposition import PCAimpor

金蝶云星空任意文件读取漏洞复现(0day)

0x01 产品简介 金蝶云星空是一款云端企业资源管理&#xff08;ERP&#xff09;软件&#xff0c;为企业提供财务管理、供应链管理以及业务流程管理等一体化解决方案。金蝶云星空聚焦多组织&#xff0c;多利润中心的大中型企业&#xff0c;以 “开放、标准、社交”三大特性为数字…
推荐文章