该题目来源于牛客网《剑指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
}
本文介绍了一种将二叉树转换为其镜像的技术。通过一个递归算法,可以有效地交换二叉树节点的左右子节点,从而实现二叉树的镜像变换。示例展示了输入二叉树与其镜像的对比。

412

被折叠的 条评论
为什么被折叠?



