-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path145.binary-tree-postorder-traversal.go
More file actions
56 lines (53 loc) · 1.22 KB
/
145.binary-tree-postorder-traversal.go
File metadata and controls
56 lines (53 loc) · 1.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
* @lc app=leetcode id=145 lang=golang
*
* [145] Binary Tree Postorder Traversal
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// func postorderTraversal(root *TreeNode) []int {
// // 非递归,后续遍历
// if root == nil{
// return []int{}
// }
// rnt := []int{}
// lefts := postorderTraversal(root.Left)
// rights := postorderTraversal(root.Right)
// rnt = append(rnt, lefts...)// 注意...不能少
// rnt = append(rnt, rights...)
// rnt = append(rnt, root.Val)
// return rnt
// }
// 非递归,后序,1个栈
func postorderTraversal(root *TreeNode) []int {
if root == nil{
return []int{}
}
var top *TreeNode = nil
stack := []*TreeNode{}
rnt := []int{}
// 头节点入栈
stack = append(stack, root)
for len(stack) > 0{
top = stack[len(stack)-1]
if top.Left!=nil && top.Left!=root && top.Right!=root{
stack = append(stack, top.Left)
}else if top.Right!=nil && top.Right!=root{
stack = append(stack, top.Right)
}else{
//访问栈顶元素
root = top
// 出栈
top,stack = stack[len(stack)-1],stack[:len(stack)-1]
// 访问站定元素
rnt = append(rnt, top.Val)
}
}
return rnt
}