二叉树的镜像
class node:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def mirror(root):
if not root:
return None
root.left, root.right = root.right, root.left
if root.left:
mirror(root.left)
if root.right:
mirror(root.right)
测试
node1 = node(1)
node2 = node(2)
node3 = node(3)
node1.left = node2
node1.right = node3
mirror(node1)
node1.left.val
3