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

相关文章

深入理解Mysql OnlineDDL的算法

《深入理解MysqlOnlineDDL的算法》本文主要介绍了讲解MysqlOnlineDDL的算法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小... 目录一、Online DDL 是什么?二、Online DDL 的三种主要算法2.1COPY(复制法)

Java集合之Iterator迭代器实现代码解析

《Java集合之Iterator迭代器实现代码解析》迭代器Iterator是Java集合框架中的一个核心接口,位于java.util包下,它定义了一种标准的元素访问机制,为各种集合类型提供了一种统一的... 目录一、什么是Iterator二、Iterator的核心方法三、基本使用示例四、Iterator的工

Java 线程池+分布式实现代码

《Java线程池+分布式实现代码》在Java开发中,池通过预先创建并管理一定数量的资源,避免频繁创建和销毁资源带来的性能开销,从而提高系统效率,:本文主要介绍Java线程池+分布式实现代码,需要... 目录1. 线程池1.1 自定义线程池实现1.1.1 线程池核心1.1.2 代码示例1.2 总结流程2. J

JS纯前端实现浏览器语音播报、朗读功能的完整代码

《JS纯前端实现浏览器语音播报、朗读功能的完整代码》在现代互联网的发展中,语音技术正逐渐成为改变用户体验的重要一环,下面:本文主要介绍JS纯前端实现浏览器语音播报、朗读功能的相关资料,文中通过代码... 目录一、朗读单条文本:① 语音自选参数,按钮控制语音:② 效果图:二、朗读多条文本:① 语音有默认值:②

Vue实现路由守卫的示例代码

《Vue实现路由守卫的示例代码》Vue路由守卫是控制页面导航的钩子函数,主要用于鉴权、数据预加载等场景,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着... 目录一、概念二、类型三、实战一、概念路由守卫(Navigation Guards)本质上就是 在路

uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)

《uni-app小程序项目中实现前端图片压缩实现方式(附详细代码)》在uni-app开发中,文件上传和图片处理是很常见的需求,但也经常会遇到各种问题,下面:本文主要介绍uni-app小程序项目中实... 目录方式一:使用<canvas>实现图片压缩(推荐,兼容性好)示例代码(小程序平台):方式二:使用uni

JAVA实现Token自动续期机制的示例代码

《JAVA实现Token自动续期机制的示例代码》本文主要介绍了JAVA实现Token自动续期机制的示例代码,通过动态调整会话生命周期平衡安全性与用户体验,解决固定有效期Token带来的风险与不便,感兴... 目录1. 固定有效期Token的内在局限性2. 自动续期机制:兼顾安全与体验的解决方案3. 总结PS

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Python屏幕抓取和录制的详细代码示例

《Python屏幕抓取和录制的详细代码示例》随着现代计算机性能的提高和网络速度的加快,越来越多的用户需要对他们的屏幕进行录制,:本文主要介绍Python屏幕抓取和录制的相关资料,需要的朋友可以参考... 目录一、常用 python 屏幕抓取库二、pyautogui 截屏示例三、mss 高性能截图四、Pill

使用MapStruct实现Java对象映射的示例代码

《使用MapStruct实现Java对象映射的示例代码》本文主要介绍了使用MapStruct实现Java对象映射的示例代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、什么是 MapStruct?二、实战演练:三步集成 MapStruct第一步:添加 Mave