代码随想录算法训练营第十三天|144. 二叉树的前序遍历、145.二叉树的后序遍历、94.二叉树的中序遍历

本文主要是介绍代码随想录算法训练营第十三天|144. 二叉树的前序遍历、145.二叉树的后序遍历、94.二叉树的中序遍历,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Leetcode144. 二叉树的前序遍历

题目链接:144. 二叉树的前序遍历

C++:

方法一:递归
/*** Definition for a binary tree node.* struct TreeNode {*     int val;*     TreeNode *left;*     TreeNode *right;*     TreeNode() : val(0), left(nullptr), right(nullptr) {}*     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}*     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/
class Solution {
public:void qianbianli(TreeNode *cur, vector<int> &result){if(cur == nullptr) return;result.push_back(cur->val);qianbianli(cur->left, result);qianbianli(cur->right, result);}vector<int> preorderTraversal(TreeNode* root) {vector<int> result;qianbianli(root, result);return result;}
};
方法二:迭代法,用栈实现前序遍历
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {//迭代法前序遍历vector<int> result;stack<TreeNode*> st;st.push(root);while(!st.empty()){TreeNode* cur = st.top();st.pop();if(cur == nullptr)continue;result.push_back(cur->val);st.push(cur->right);st.push(cur->left);}return result;}
};
统一迭代: 
class Solution {
public:vector<int> preorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;if(root != nullptr) st.push(root);while(!st.empty()){TreeNode *node = st.top();if(node != nullptr){st.pop();if(node->right) st.push(node->right);if(node->left) st.push(node->left);st.push(node);st.push(nullptr);}else{st.pop();node = st.top();st.pop();result.push_back(node->val);}}return result;}
};

Python:

递归法:
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:def qianbianli(self, cur, result):if cur == None:returnresult.append(cur.val)self.qianbianli(cur.left, result)self.qianbianli(cur.right, result)def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:result = []self.qianbianli(root, result)return result
迭代法:
class Solution:def preorderTraversal(self, root: Optional[TreeNode]) -> List[int]:result = []stack = [root]while stack:cur = stack.pop()if cur == None:continueresult.append(cur.val)stack.append(cur.right)stack.append(cur.left)return result

Leetcode145.二叉树的后序遍历

题目链接:145. 二叉树的后序遍历

C++:

递归法:
class Solution {
public:void houbianli(TreeNode *cur, vector<int> &result){if(cur == nullptr) return;houbianli(cur->left, result);houbianli(cur->right, result);result.push_back(cur->val);}vector<int> postorderTraversal(TreeNode* root) {vector<int> result;houbianli(root, result);return result;}
};
迭代法: 
class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;st.push(root);while(!st.empty()){TreeNode *cur = st.top();st.pop();if(cur == nullptr)continue;result.push_back(cur->val);st.push(cur->left);st.push(cur->right);}reverse(result.begin(), result.end());return result;}
};
统一迭代:  
class Solution {
public:vector<int> postorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;if(root != nullptr) st.push(root);while(!st.empty()){TreeNode *node = st.top();if(node != nullptr){st.pop();st.push(node);                          //中st.push(nullptr);if(node->right) st.push(node->right);   //右if(node->left) st.push(node->left);     //左}else{st.pop();node = st.top();st.pop();result.push_back(node->val);}}return result;}
};

Python:

递归法:
class Solution:def houbianli(self, cur, result):if cur == None:returnself.houbianli(cur.left, result)self.houbianli(cur.right, result)result.append(cur.val)def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:result = []self.houbianli(root, result)return result
迭代法:
class Solution:def postorderTraversal(self, root: Optional[TreeNode]) -> List[int]:result = []stack = [root]while stack:cur = stack.pop()if cur == None:continueresult.append(cur.val)stack.append(cur.left)stack.append(cur.right)return result[::-1]

Leetcode94.二叉树的中序遍历

题目链接:94. 二叉树的中序遍历

C++:

递归法:
class Solution {
public:void zhongbianli(TreeNode* cur, vector<int> &result){if(cur == nullptr)return;zhongbianli(cur->left, result);result.push_back(cur->val);zhongbianli(cur->right, result);}vector<int> inorderTraversal(TreeNode* root) {vector<int> result;zhongbianli(root, result);return result;}
};
迭代法:
class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;TreeNode *cur = root;while(!st.empty() || cur != nullptr){if(cur != nullptr){st.push(cur);cur = cur->left;}else{cur = st.top();st.pop();result.push_back(cur->val);cur = cur->right;}}return result;}
};
统一迭代: 
class Solution {
public:vector<int> inorderTraversal(TreeNode* root) {vector<int> result;stack<TreeNode*> st;if(root != nullptr) st.push(root);while(!st.empty()){TreeNode *node = st.top();if(node != nullptr){st.pop();if(node->right) st.push(node->right);st.push(node);st.push(nullptr);if(node->left) st.push(node->left);}else{st.pop();node = st.top();st.pop();result.push_back(node->val);}}return result;}
};

Python:

递归法:
class Solution:def zhongbianli(self, cur, result):if cur == None:returnself.zhongbianli(cur.left, result)result.append(cur.val)self.zhongbianli(cur.right, result)def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:result = []self.zhongbianli(root, result)return result
迭代法:
class Solution:def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:result = []st = []cur = rootwhile cur != None or st:if cur != None:st.append(cur)cur = cur.leftelse:cur = st.pop()result.append(cur.val)cur = cur.rightreturn result

这篇关于代码随想录算法训练营第十三天|144. 二叉树的前序遍历、145.二叉树的后序遍历、94.二叉树的中序遍历的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1110319

相关文章

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

MySQL存储过程之循环遍历查询的结果集详解

《MySQL存储过程之循环遍历查询的结果集详解》:本文主要介绍MySQL存储过程之循环遍历查询的结果集,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录前言1. 表结构2. 存储过程3. 关于存储过程的SQL补充总结前言近来碰到这样一个问题:在生产上导入的数据发现

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=

C#代码实现解析WTGPS和BD数据

《C#代码实现解析WTGPS和BD数据》在现代的导航与定位应用中,准确解析GPS和北斗(BD)等卫星定位数据至关重要,本文将使用C#语言实现解析WTGPS和BD数据,需要的可以了解下... 目录一、代码结构概览1. 核心解析方法2. 位置信息解析3. 经纬度转换方法4. 日期和时间戳解析5. 辅助方法二、L

Python使用Code2flow将代码转化为流程图的操作教程

《Python使用Code2flow将代码转化为流程图的操作教程》Code2flow是一款开源工具,能够将代码自动转换为流程图,该工具对于代码审查、调试和理解大型代码库非常有用,在这篇博客中,我们将深... 目录引言1nVflRA、为什么选择 Code2flow?2、安装 Code2flow3、基本功能演示

IIS 7.0 及更高版本中的 FTP 状态代码

《IIS7.0及更高版本中的FTP状态代码》本文介绍IIS7.0中的FTP状态代码,方便大家在使用iis中发现ftp的问题... 简介尝试使用 FTP 访问运行 Internet Information Services (IIS) 7.0 或更高版本的服务器上的内容时,IIS 将返回指示响应状态的数字代

MySQL 添加索引5种方式示例详解(实用sql代码)

《MySQL添加索引5种方式示例详解(实用sql代码)》在MySQL数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中,下面给大家分享MySQL添加索引5种方式示例详解(实用sql代码),... 在mysql数据库中添加索引可以帮助提高查询性能,尤其是在数据量大的表中。索引可以在创建表时定义,也可