-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
286 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestFunc(t *testing.T) { | ||
for _, c := range []struct { | ||
input1 []int | ||
expect []int | ||
}{ | ||
{[]int{1, 2, 3}, []int{1, 3, 2}}, | ||
{[]int{3, 2, 1}, []int{1, 2, 3}}, | ||
{[]int{1, 1, 5}, []int{1, 5, 1}}, | ||
} { | ||
nextPermutation(c.input1) | ||
t.Log(c, reflect.DeepEqual(c.expect, c.input1)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package main | ||
|
||
type TreeNode struct { | ||
Val int | ||
Left *TreeNode | ||
Right *TreeNode | ||
} | ||
|
||
func postorderTraversal1(root *TreeNode) []int { | ||
res := []int{} | ||
helper(root, &res) | ||
return res | ||
} | ||
func helper(root *TreeNode, ns *[]int) { | ||
if root == nil { | ||
return | ||
} | ||
helper(root.Left, ns) | ||
helper(root.Right, ns) | ||
*ns = append((*ns), root.Val) | ||
} | ||
|
||
// 迭代:visit数组 | ||
func postorderTraversal_visit(root *TreeNode) []int { | ||
if root == nil { | ||
return nil | ||
} | ||
res := []int{} | ||
stk := make([]*TreeNode, 0, 8) | ||
stk = append(stk, root) | ||
visit := make([]bool, 1, 8) // 标识是否已经释放子节点 | ||
for len(stk) > 0 { | ||
n := len(stk) | ||
root = stk[n-1] | ||
if visit[n-1] { // | ||
res = append(res, root.Val) | ||
stk = stk[:n-1] | ||
visit = visit[:n-1] | ||
continue | ||
} | ||
// 添加子节点 | ||
visit[n-1] = true | ||
if root.Right != nil { | ||
stk = append(stk, root.Right) | ||
visit = append(visit, false) | ||
} | ||
if root.Left != nil { | ||
stk = append(stk, root.Left) | ||
visit = append(visit, false) | ||
} | ||
} | ||
return res | ||
} | ||
|
||
// 迭代:无 visit数组 | ||
func postorderTraversal(root *TreeNode) []int { | ||
if root == nil { | ||
return nil | ||
} | ||
res := []int{} | ||
stk := make([]*TreeNode, 0, 8) | ||
var prev *TreeNode // 记录前一个访问的节点 | ||
for root != nil || len(stk) > 0 { | ||
// 左儿子入栈 | ||
for ; root != nil; root = root.Left { | ||
stk = append(stk, root) | ||
} | ||
n := len(stk) | ||
root = stk[n-1] | ||
// 没有右儿子则访问 | ||
// 右儿子和前一个访问节点一样则访问:回溯 | ||
if root.Right == nil || root.Right == prev { | ||
res = append(res, root.Val) | ||
stk = stk[:n-1] | ||
prev = root | ||
root = nil // 置零 | ||
} else { | ||
root = root.Right | ||
} | ||
} | ||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package main | ||
|
||
import ( | ||
"container/list" | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func makeTree(arr []int) *TreeNode { | ||
l := len(arr) | ||
if l == 0 { | ||
return nil | ||
} | ||
var mk func(i int) *TreeNode | ||
mk = func(i int) *TreeNode { | ||
if i >= l || arr[i] == 0 { | ||
return nil | ||
} | ||
root := &TreeNode{arr[i], nil, nil} | ||
root.Left = mk(2*i + 1) | ||
root.Right = mk(2*i + 2) | ||
return root | ||
} | ||
return mk(0) | ||
} | ||
|
||
func resovleTree(t *TreeNode) [][]int { | ||
l := list.New() | ||
l.PushBack(t) | ||
res := make([][]int, 0, 4) | ||
for l.Len() != 0 { | ||
v := l.Remove(l.Front()).(*TreeNode) | ||
tmp := []int{v.Val, -1, -1} | ||
if v.Left != nil { | ||
l.PushBack(v.Left) | ||
tmp[1] = v.Left.Val | ||
} | ||
if v.Right != nil { | ||
l.PushBack(v.Right) | ||
tmp[2] = v.Right.Val | ||
} | ||
res = append(res, tmp) | ||
} | ||
return res | ||
} | ||
|
||
func TestFunc(t *testing.T) { | ||
for _, c := range []struct { | ||
input1 []int | ||
expect []int | ||
}{ | ||
{[]int{1, 2, 5, 3, 4, 0, 6}, []int{3, 4, 2, 6, 5, 1}}, | ||
{[]int{1, 0, 2, 0, 0, 3}, []int{3, 2, 1}}, | ||
} { | ||
tree := makeTree(c.input1) | ||
v := postorderTraversal(tree) | ||
t.Log(c, v, reflect.DeepEqual(c.expect, v)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters