Skip to content

Commit

Permalink
feat(590): Easy
Browse files Browse the repository at this point in the history
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
DziedzicGrzegorz committed Aug 26, 2024
1 parent ee396f5 commit c63bb6b
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
21 changes: 21 additions & 0 deletions internal/Leetcode/590/590.go
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
}
50 changes: 50 additions & 0 deletions internal/Leetcode/590/590_test.go
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)
}
})
}
}

0 comments on commit c63bb6b

Please sign in to comment.