-
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.
Same as https://github.com/DziedzicGrzegorz/GoPatterns/blob/55ac301d7124fa145b79d5fb8ed754cca02fbde4/internal/Leetcode/145/145.go but multiple nodes BREAKING CHANGE: Remember the following text: "It can be faster with a non-recursive solution.
- Loading branch information
1 parent
ee396f5
commit c63bb6b
Showing
2 changed files
with
71 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package _590 | ||
|
||
type Node struct { | ||
Val int | ||
Children []*Node | ||
} | ||
|
||
func postorder(root *Node) (ans []int) { | ||
var dfs func(*Node) | ||
dfs = func(root *Node) { | ||
if root == nil { | ||
return | ||
} | ||
for _, child := range root.Children { | ||
dfs(child) | ||
} | ||
ans = append(ans, root.Val) | ||
} | ||
dfs(root) | ||
return | ||
} |
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,50 @@ | ||
package _590 | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_postorder(t *testing.T) { | ||
type args struct { | ||
root *Node | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want []int | ||
}{ | ||
{ | ||
name: "Example 1", | ||
args: args{ | ||
root: &Node{ | ||
Val: 1, | ||
Children: []*Node{ | ||
{ | ||
Val: 3, | ||
Children: []*Node{ | ||
{ | ||
Val: 5, | ||
Children: nil, | ||
}, | ||
{ | ||
|
||
Val: 6, | ||
Children: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
want: []int{5, 6, 3, 1}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := postorder(tt.args.root); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("postorder() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |