diff options
-rw-r--r-- | 226/go.mod | 3 | ||||
-rw-r--r-- | 226/main.go | 42 |
2 files changed, 45 insertions, 0 deletions
diff --git a/226/go.mod b/226/go.mod new file mode 100644 index 0000000..9a2c826 --- /dev/null +++ b/226/go.mod @@ -0,0 +1,3 @@ +module 226 + +go 1.19 diff --git a/226/main.go b/226/main.go new file mode 100644 index 0000000..da66748 --- /dev/null +++ b/226/main.go @@ -0,0 +1,42 @@ +package main + +import "fmt" + +type TreeNode struct { + Val int + Left *TreeNode + Right *TreeNode +} + +func invertTree(root *TreeNode) *TreeNode { + var invert func(root *TreeNode) + invert = func(root *TreeNode) { + if root == nil { + return + } + + root.Left, root.Right = root.Right, root.Left + invert(root.Left) + invert(root.Right) + } + invert(root) + + return root +} + +// func invertTree(root *TreeNode) *TreeNode { +// if root == nil { +// return nil +// } + +// right := invertTree(root.Right) +// left := invertTree(root.Left) +// root.Left = right +// root.Right = left + +// return root +// } + +func main() { + fmt.Println("vim-go") +} |