代码随想录算法训练营第十三天|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

相关文章

使用 sql-research-assistant进行 SQL 数据库研究的实战指南(代码实现演示)

《使用sql-research-assistant进行SQL数据库研究的实战指南(代码实现演示)》本文介绍了sql-research-assistant工具,该工具基于LangChain框架,集... 目录技术背景介绍核心原理解析代码实现演示安装和配置项目集成LangSmith 配置(可选)启动服务应用场景

Python中顺序结构和循环结构示例代码

《Python中顺序结构和循环结构示例代码》:本文主要介绍Python中的条件语句和循环语句,条件语句用于根据条件执行不同的代码块,循环语句用于重复执行一段代码,文章还详细说明了range函数的使... 目录一、条件语句(1)条件语句的定义(2)条件语句的语法(a)单分支 if(b)双分支 if-else(

MySQL数据库函数之JSON_EXTRACT示例代码

《MySQL数据库函数之JSON_EXTRACT示例代码》:本文主要介绍MySQL数据库函数之JSON_EXTRACT的相关资料,JSON_EXTRACT()函数用于从JSON文档中提取值,支持对... 目录前言基本语法路径表达式示例示例 1: 提取简单值示例 2: 提取嵌套值示例 3: 提取数组中的值注意

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

JAVA调用Deepseek的api完成基本对话简单代码示例

《JAVA调用Deepseek的api完成基本对话简单代码示例》:本文主要介绍JAVA调用Deepseek的api完成基本对话的相关资料,文中详细讲解了如何获取DeepSeekAPI密钥、添加H... 获取API密钥首先,从DeepSeek平台获取API密钥,用于身份验证。添加HTTP客户端依赖使用Jav

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

nginx-rtmp-module模块实现视频点播的示例代码

《nginx-rtmp-module模块实现视频点播的示例代码》本文主要介绍了nginx-rtmp-module模块实现视频点播,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习... 目录预置条件Nginx点播基本配置点播远程文件指定多个播放位置参考预置条件配置点播服务器 192.

CSS自定义浏览器滚动条样式完整代码

《CSS自定义浏览器滚动条样式完整代码》:本文主要介绍了如何使用CSS自定义浏览器滚动条的样式,包括隐藏滚动条的角落、设置滚动条的基本样式、轨道样式和滑块样式,并提供了完整的CSS代码示例,通过这些技巧,你可以为你的网站添加个性化的滚动条样式,从而提升用户体验,详细内容请阅读本文,希望能对你有所帮助...

深入解析Spring TransactionTemplate 高级用法(示例代码)

《深入解析SpringTransactionTemplate高级用法(示例代码)》TransactionTemplate是Spring框架中一个强大的工具,它允许开发者以编程方式控制事务,通过... 目录1. TransactionTemplate 的核心概念2. 核心接口和类3. TransactionT

Java实现Elasticsearch查询当前索引全部数据的完整代码

《Java实现Elasticsearch查询当前索引全部数据的完整代码》:本文主要介绍如何在Java中实现查询Elasticsearch索引中指定条件下的全部数据,通过设置滚动查询参数(scrol... 目录需求背景通常情况Java 实现查询 Elasticsearch 全部数据写在最后需求背景通常情况下