版权声明:原创勿转 https://cloud.tencent.com/developer/article/1412918
思路
传说中的反转二叉树,递归交换
code
代码语言:javascript复制type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func invertTree(root *TreeNode) *TreeNode {
if root == nil {
return root
}
root.Left = invertTree(root.Left)
root.Right = invertTree(root.Right)
root.Left, root.Right = root.Right, root.Left
return root
}