From 49a72dcf24ba4f8d6b4f3beecc9e6d864fd8bb81 Mon Sep 17 00:00:00 2001 From: terminaldweller Date: Sat, 18 Feb 2023 07:31:24 +0330 Subject: 226 --- 226/go.mod | 3 +++ 226/main.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 226/go.mod create mode 100644 226/main.go (limited to '226') 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") +} -- cgit v1.2.3