Golang Leetcode 226. Invert Binary Tree.go

2019-04-12 11:35:02 浏览数 (1)

版权声明:原创勿转 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
}

0 人点赞