二叉树的镜像

 

 

 

https://leetcode.cn/problems/er-cha-shu-de-jing-xiang-lcof/

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func mirrorTree(root *TreeNode) *TreeNode {
    ans:=root
    doTask(root)
    return ans
}
func doTask(root *TreeNode) {
    if root==nil{
        return
    }

    tmp:=root.Left
    root.Left=root.Right
    root.Right=tmp
    doTask(root.Left)
    doTask(root.Right)
}

 

posted @ 2022-06-30 21:01  知道了呀~  阅读(40)  评论(0编辑  收藏  举报