homebrew作者都不会的题目
Invert a binary tree.
Example:
Input:
4 / \ 2 7 / \ / \ 1 3 6 9
Output:
4 / \ 7 2 / \ / \ 9 6 3 1
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func invertTree(root *TreeNode) *TreeNode {
if root==nil{
return root;
}
var temp *TreeNode
temp=root.Left
root.Left=root.Right
root.Right=temp
root.Left=invertTree(root.Left)
root.Right=invertTree(root.Right)
return root
}