简言之,已知二叉树,求其镜像二叉树:
思路:从根结点开始递归,若该节点不为None,则将左右子结点交换,递归左右子节点。
python代码:
# -*- 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 not root:
return root
tmp_node = root.left
root.left = root.right
root.right = tmp_node
self.Mirror(root.left)
self.Mirror(root.right)
return root