Go语言实现:【剑指offer】二叉树的镜像

该题目来源于牛客网《剑指offer》专题。

操作给定的二叉树,将其变换为源二叉树的镜像。

示例:
输入:
4
/
2 7
/ /
1 3 6 9

输出:
4
/
7 2
/ /
9 6 3 1

Go语言实现:

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func invertTree(root *TreeNode) *TreeNode {
    if root == nil {
        return nil
    }
    
    root.Left, root.Right = root.Right, root.Left
    invertTree(root.Left)
    invertTree(root.Right)
    
    return root
}
原文地址:https://www.cnblogs.com/dubinyang/p/12099392.html