@@ -299,108 +299,96 @@ class Solution:
299
299
300
300
301
301
Go:
302
+
302
303
> 迭代法前序遍历
303
304
304
305
```go
305
- // 迭代法前序遍历
306
- /* *
307
- type Element struct {
308
- // 元素保管的值
309
- Value interface{}
310
- // 内含隐藏或非导出字段
311
- }
312
-
313
- func (l *List) Back() *Element
314
- 前序遍历:中左右
315
- 压栈顺序:右左中
316
- **/
317
306
func preorderTraversal (root * TreeNode) [ ] int {
307
+ ans := [ ] int{}
308
+
318
309
if root == nil {
319
- return nil
310
+ return ans
320
311
}
321
- var stack = list.New()
322
- stack.PushBack(root.Right)
323
- stack.PushBack(root.Left)
324
- res:=[ ] int{}
325
- res=append(res,root.Val)
326
- for stack.Len()>0 {
327
- e:=stack.Back()
328
- stack.Remove(e)
329
- node := e.Value.(* TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
330
- if node==nil{
331
- continue
312
+
313
+ st := list.New()
314
+ st.PushBack(root)
315
+
316
+ for st.Len() > 0 {
317
+ node := st.Remove(st.Back()).(*TreeNode)
318
+
319
+ ans = append(ans, node.Val)
320
+ if node.Right != nil {
321
+ st.PushBack(node.Right)
322
+ }
323
+ if node.Left != nil {
324
+ st.PushBack(node.Left)
332
325
}
333
- res=append(res,node.Val)
334
- stack.PushBack(node.Right)
335
- stack.PushBack(node.Left)
336
326
}
337
- return res
327
+ return ans
338
328
}
339
329
```
340
330
341
331
> 迭代法后序遍历
342
332
343
333
```go
344
- //迭代法后序遍历
345
- //后续遍历:左右中
346
- //压栈顺序:中右左(按照前序遍历思路),再反转结果数组
347
334
func postorderTraversal(root *TreeNode) []int {
335
+ ans := []int{}
336
+
348
337
if root == nil {
349
- return nil
338
+ return ans
350
339
}
351
- var stack = list.New()
352
- stack.PushBack(root.Left)
353
- stack.PushBack(root.Right)
354
- res:=[]int{}
355
- res=append(res,root.Val)
356
- for stack.Len()>0 {
357
- e:=stack.Back()
358
- stack.Remove(e)
359
- node := e.Value.(*TreeNode)//e是Element类型,其值为e.Value.由于Value为接口,所以要断言
360
- if node==nil{
361
- continue
340
+
341
+ st := list.New()
342
+ st.PushBack(root)
343
+
344
+ for st.Len() > 0 {
345
+ node := st.Remove(st.Back()).(*TreeNode)
346
+
347
+ ans = append(ans, node.Val)
348
+ if node.Left != nil {
349
+ st.PushBack(node.Left)
350
+ }
351
+ if node.Right != nil {
352
+ st.PushBack(node.Right)
362
353
}
363
- res=append(res,node.Val)
364
- stack.PushBack(node.Left)
365
- stack.PushBack(node.Right)
366
354
}
367
- for i:=0;i<len(res)/2;i++{
368
- res[i],res[len(res)-i-1] = res[len(res)-i-1],res[i]
355
+ reverse(ans)
356
+ return ans
357
+ }
358
+
359
+ func reverse(a []int) {
360
+ l, r := 0, len(a) - 1
361
+ for l < r {
362
+ a[l], a[r] = a[r], a[l]
363
+ l, r = l+1, r-1
369
364
}
370
- return res
371
365
}
372
366
```
373
367
374
368
> 迭代法中序遍历
375
369
376
370
``` go
377
- // 迭代法中序遍历
378
371
func inorderTraversal (root *TreeNode ) []int {
379
- rootRes := []int {}
380
- if root== nil {
381
- return nil
372
+ ans := []int {}
373
+ if root == nil {
374
+ return ans
382
375
}
383
- stack := list.New ()
384
- node := root
385
- // 先将所有左节点找到,加入栈中
386
- for node!=nil {
387
- stack.PushBack (node)
388
- node=node.Left
389
- }
390
- // 其次对栈中的每个节点先弹出加入到结果集中,再找到该节点的右节点的所有左节点加入栈中
391
- for stack.Len ()>0 {
392
- e := stack.Back ()
393
- node := e.Value .(*TreeNode)
394
- stack.Remove (e)
395
- // 找到该节点的右节点,再搜索他的所有左节点加入栈中
396
- rootRes=append (rootRes,node.Val )
397
- node=node.Right
398
- for node!=nil {
399
- stack.PushBack (node)
400
- node=node.Left
376
+
377
+ st := list.New ()
378
+ cur := root
379
+
380
+ for cur != nil || st.Len () > 0 {
381
+ if cur != nil {
382
+ st.PushBack (cur)
383
+ cur = cur.Left
384
+ } else {
385
+ cur = st.Remove (st.Back ()).(*TreeNode)
386
+ ans = append (ans, cur.Val )
387
+ cur = cur.Right
401
388
}
402
389
}
403
- return rootRes
390
+
391
+ return ans
404
392
}
405
393
```
406
394
0 commit comments