剑指 Offer 27. 二叉树的镜像
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def mirrorTree(self, root: TreeNode) -> TreeNode:
#这个问题为从上到下交换左右节点,然后递归下去
def mirrorLeftRight(tmpRoot):
# if not tmpRoot.left and not tmpRoot.right:
if not tmpRoot:
return
middleNode = tmpRoot.left
tmpRoot.left = tmpRoot.right
tmpRoot.right = middleNode
mirrorLeftRight(tmpRoot.left)
mirrorLeftRight(tmpRoot.right)
return
if not root:
return root
returnRoot = root
mirrorLeftRight(root)
return returnRoot