From cc510357c1fe6f0cf25fd4e800c2a89b03b4659f Mon Sep 17 00:00:00 2001 From: jinbudaily <18336218010@163.com> Date: Thu, 20 Jul 2023 16:22:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=A0=91=E7=9A=84=E7=BB=9F=E4=B8=80=E8=BF=AD=E4=BB=A3=E6=B3=95?= =?UTF-8?q?=20=E6=8E=92=E7=89=88=E6=A0=BC=E5=BC=8F=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- problems/二叉树的统一迭代法.md | 34 ++++++++++++------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/problems/二叉树的统一迭代法.md b/problems/二叉树的统一迭代法.md index 27464269..8089af64 100644 --- a/problems/二叉树的统一迭代法.md +++ b/problems/二叉树的统一迭代法.md @@ -5,10 +5,11 @@

参与本项目,贡献其他语言版本的代码,拥抱开源,让更多学习算法的小伙伴们收益!

+> 统一写法是一种什么感觉 # 二叉树的统一迭代法 -> 统一写法是一种什么感觉 +## 思路 此时我们在[二叉树:一入递归深似海,从此offer是路人](https://programmercarl.com/二叉树的递归遍历.html)中用递归的方式,实现了二叉树前中后序的遍历。 @@ -28,7 +29,7 @@ 如何标记呢,**就是要处理的节点放入栈之后,紧接着放入一个空指针作为标记。** 这种方法也可以叫做标记法。 -## 迭代法中序遍历 +### 迭代法中序遍历 中序遍历代码如下:(详细注释) @@ -71,7 +72,7 @@ public: 此时我们再来看前序遍历代码。 -## 迭代法前序遍历 +### 迭代法前序遍历 迭代法前序遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -102,7 +103,7 @@ public: }; ``` -## 迭代法后序遍历 +### 迭代法后序遍历 后续遍历代码如下: (**注意此时我们和中序遍历相比仅仅改变了两行代码的顺序**) @@ -143,15 +144,11 @@ public: 所以大家根据自己的个人喜好,对于二叉树的前中后序遍历,选择一种自己容易理解的递归和迭代法。 +## 其他语言版本 - - - -# 其他语言版本 - - -Java: +### Java: 迭代法前序遍历代码如下: + ```java class Solution { public List preorderTraversal(TreeNode root) { @@ -235,7 +232,7 @@ class Solution { } ``` -Python: +### Python: 迭代法前序遍历: ```python @@ -309,7 +306,8 @@ class Solution: return result ``` -Go: +### Go: + > 前序遍历统一迭代法 ```GO @@ -442,7 +440,7 @@ func postorderTraversal(root *TreeNode) []int { } ``` -javaScript: +### JavaScript: > 前序遍历统一迭代法 @@ -522,7 +520,7 @@ var postorderTraversal = function(root, res = []) { ``` -TypeScript: +### TypeScript: ```typescript // 前序遍历(迭代法) @@ -591,7 +589,8 @@ function postorderTraversal(root: TreeNode | null): number[] { return res; }; ``` -Scala: +### Scala: + ```scala // 前序遍历 object Solution { @@ -667,7 +666,7 @@ object Solution { } ``` -rust: +### Rust: ```rust impl Solution{ @@ -747,3 +746,4 @@ impl Solution{ +