[剑指Offer]二叉树的镜像

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

代码

Python(2.7.3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# 返回镜像树的根节点
def Mirror(self, root):
# write code here
if root is not None:
# Python可以直接在一行交换两个变量的值,不必引入新变量
root.left, root.right = root.right, root.left
self.Mirror(root.left)
self.Mirror(root.right)
return root

运行时间:31ms
占用内存:5848k

参考

https://www.nowcoder.com/profile/6927081/codeBookDetail?submissionId=12712193