Skip to content

Commit

Permalink
invert binary tree
Browse files Browse the repository at this point in the history
  • Loading branch information
alastairruhm committed Apr 10, 2018
1 parent a65de9a commit 53b31ae
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions invertBinaryTree/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://leetcode.com/problems/invert-binary-tree/hints/

Invert a binary tree.
28 changes: 28 additions & 0 deletions invertBinaryTree/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package invertBinaryTree

/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/

// TreeNode definition of binary tree
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}

func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return nil
}
root.Left = invertTree(root.Left)
root.Right = invertTree(root.Right)

root.Left, root.Right = root.Right, root.Left
return root
}

0 comments on commit 53b31ae

Please sign in to comment.