@@ -74,41 +74,19 @@ https://leetcode-cn.com/problems/binary-tree-inorder-traversal/
7474JavaScript Code:
7575
7676``` js
77- /**
78- * @param {TreeNode} root
79- * @return {number[]}
80- */
8177var inorderTraversal = function (root ) {
82- // 1. Recursive solution
83- // if (!root) return [];
84- // const left = root.left ? inorderTraversal(root.left) : [];
85- // const right = root.right ? inorderTraversal(root.right) : [];
86- // return left.concat([root.val]).concat(right);
87-
88- // 2. iterative solutuon
89- if (! root) return [];
90- const stack = [root];
91- const ret = [];
92- let left = root .left ;
93-
94- let item = null ; // stack 中弹出的当前项
95-
96- while (left) {
97- stack .push (left);
98- left = left .left ;
99- }
100-
101- while ((item = stack .pop ())) {
102- ret .push (item .val );
103- let t = item .right ;
104-
105- while (t) {
106- stack .push (t);
107- t = t .left ;
78+ const res = [];
79+ const stk = [];
80+ while (root || stk .length ) {
81+ while (root) {
82+ stk .push (root);
83+ root = root .left ;
10884 }
85+ root = stk .pop ();
86+ res .push (root .val );
87+ root = root .right ;
10988 }
110-
111- return ret;
89+ return res;
11290};
11391```
11492
@@ -143,46 +121,22 @@ public:
143121
144122Python Code:
145123
146- ```Python
147- # Definition for a binary tree node.
148- # class TreeNode:
149- # def __init__(self, x):
150- # self.val = x
151- # self.left = None
152- # self.right = None
153-
124+ ```py
154125class Solution:
155126 def inorderTraversal(self, root: TreeNode) -> List[int]:
156- """
157- 1. 递归法可以一行代码完成,无需讨论;
158- 2. 迭代法一般需要通过一个栈保存节点顺序,我们这里直接使用列表
159- - 首先,我要按照中序遍历的顺序存入栈,这边用的逆序,方便从尾部开始处理
160- - 在存入栈时加入一个是否需要深化的参数
161- - 在回头取值时,这个参数应该是否,即直接取值
162- - 简单调整顺序,即可实现前序和后序遍历
163- """
164- # 递归法
165- # if root is None:
166- # return []
167- # return self.inorderTraversal(root.left)\
168- # + [root.val]\
169- # + self.inorderTraversal(root.right)
170- # 迭代法
171- result = []
172- stack = [(1, root)]
173- while stack:
174- go_deeper, node = stack.pop()
175- if node is None:
176- continue
177- if go_deeper:
178- # 左右节点还需继续深化,并且入栈是先右后左
179- stack.append((1, node.right))
180- # 节点自身已遍历,回头可以直接取值
181- stack.append((0, node))
182- stack.append((1, node.left))
183- else:
184- result.append(node.val)
185- return result
127+ if not root: return []
128+ stack = []
129+ ans = []
130+ cur = root
131+
132+ while cur or stack:
133+ while cur:
134+ stack.append(cur)
135+ cur = cur.left
136+ cur = stack.pop()
137+ ans.append(cur.val)
138+ cur = cur.right
139+ return ans
186140```
187141
188142Java Code:
@@ -253,7 +207,6 @@ class Solution {
253207
254208- [ 二叉树的遍历] ( https://github.com/azl397985856/leetcode/blob/master/thinkings/binary-tree-traversal.md )
255209
256-
257210大家对此有何看法,欢迎给我留言,我有时间都会一一查看回答。更多算法套路可以访问我的 LeetCode 题解仓库:https://github.com/azl397985856/leetcode 。 目前已经 37K star 啦。
258211大家也可以关注我的公众号《力扣加加》带你啃下算法这块硬骨头。
259- ![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg )
212+ ![ ] ( https://tva1.sinaimg.cn/large/007S8ZIlly1gfcuzagjalj30p00dwabs.jpg )
0 commit comments