前序遍历法
按照前序遍历的框架,在前序遍历的编码区域处,直接交换左右节点。
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#
# @param pRoot TreeNode类
# @return TreeNode类
#
class Solution:
def Mirror(self , pRoot: TreeNode) -> TreeNode:
if not pRoot: return
temp = pRoot.left
pRoot.left = pRoot.right
pRoot.right = temp
# 前序遍历
self.Mirror(pRoot.left)
self.Mirror(pRoot.right)
return pRoot